C Program to Print All Prime Numbers between 1 and 100
C Program to Print All Prime Numbers between 1 and 100
Problem Description
This simple C program prints all the prime numbers between 1 and 100. A Prime number is a natural number greater than 1 that is only divisible by either 1 or itself. All numbers other than prime numbers are known as composite numbers. There are infinitely many prime numbers, here is the list of first few prime numbers 2 3 5 7 11 13 17 19 23 29 31 37.... This C program illustrates the use of nested for loops. The outer for loop iterate from 2 to 100 while the inner loop check the number for prime condition.
C Program to Print All Prime Numbers between 1 and 100 - Source code
/*C program to print all the prime numbers between 1 and 100*/ #include<stdio.h> int main() { int number,j,flag; printf(" The prime numbers between 1 and 100 : \n"); for(number=2;number<=100;number++) { flag=0; for(j=2;j<=number/2;j++) { if(number % j == 0){ flag = 1; break; } } if(flag==0) printf("\n %d ",number); } return 0; }
Program Output
The prime numbers between 1 and 100 : 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
Program Explanation
1. This C program uses nested for loops to find and print all the prime numbers between 1 and 100. 2. The outer loop iterate through 2 to 100, while the inner loop use the modulus operator to check whether the number in current iteration is prime or not. If a prime number is found, the variable flag is set to 1 and the inner loop is existed using the break statement. 3. Otherwise, if flag variable remains zero, the number in current iteration is displayed on the screen as prime number.