C program to print prime numbers from 1 to 100. If you are looking for C program to find prime numbers, here in this tutorial we will help you to learn how to write a C program for prime numbers.
C Program to Print Prime Numbers
Learn how to write a C program to print all Prime numbers between 1 to 100. Writing a C program to display prime numbers between two intervals can be done using various techniques but here in this program, we show how to write a C program to print prime numbers from 1 to 100 in a proper way.
C Program to Print Prime Numbers Source Code
/* C Program to print prime numbers from 1 to 100 - PrimeNumbers.C */ #include <stdio.h> #include <stdlib.h> void main() { //variable declaration int num1, num2, i, j, flag, temp, count = 0; //asking user to enter lower and upper limit numbers and reading printf("Enter the value of num1 and num2 \n"); scanf("%d %d", &num1, &num2); //logic to finding and printing prime numbers from 1 to 100 if (num2 < 2) { printf("There are no primes upto %d\n", num2); exit(0); } printf("Prime numbers are \n"); temp = num1; if ( num1 % 2 == 0) { num1++; } for (i = num1; i <= num2; i = i + 2) { flag = 0; for (j = 2; j <= i / 2; j++) { if ((i % j) == 0) { flag = 1; break; } } if (flag == 0) { printf("%d\n", i); count++; } } printf("Number of primes between %d & %d = %d\n", temp, num2, count); }
C Program to Print Prime Numbers Output
Enter the value of num1 and num2
1
100
Prime numbers are
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61
67 71 73 79 83 89 97
Number of primes between 1 & 100 = 25
C PROGRAMMING TUTORIALS
- C Program to Find LCM and GCD of Two Numbers
- C Program to Add Spaces in a String
- C Program To Add Digits Of A Number
- C Program To Find Leap Year
- C Program To Append Data Into A File
- C Program to Accept Only Integer Values
- C Program To Arrange Numbers In Descending Order
- C Program to Add Days to Date
- C Program to Add Two Fractions
- C Program To Reverse A Number
- C Program to Find Maximum and Minimum Numbers
- C Program to Read a Text File
- C Program to Convert Decimal to Hexadecimal
- C Program to Convert Decimal to Binary
- C Program to Convert Celsius to Fahrenheit
- C Program To Find Absolute Value
- Ternary Operator Program in C
- C Program For Addition Table Using For Loop
- Denomination Program in C
- C Program to Print Multiplication Table