In this example, you will learn How to Display Prime Numbers in C
A prime number is any number that has two integer and positive divisors that are 1 and the number itself. 1 is not considered a prime number because it admits a divisor. The 0 also since it is divisible by all the numbers.
Program that displays all prime numbers less than n:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<stdio.h> #include<stdlib.h> int main() { int number=1, counter=0; int i,r,n=100; printf("Enter N:"); scanf("%d",&n); printf("Prime numbers less than %d are: \n",n); while(number <n){//as a number <n then r=0;// to count the number of divisors number++; for (i=1 ; i<=number ; i++) { if ((number%i)==0) r++; } if(r==2)// The prime number is divided on 1 and on itself { printf(" %d \n",number); } } system("pause"); } |
Output:
In this example, you have learned How to Display Prime Numbers in C
C program to Display Prime Numbers between 1 to 1000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<stdio.h> #include<stdlib.h> int main() { int number=1, counter=0; int i,r,n=1000; printf("Prime numbers less than %d are: \n",n); while(number <n){//as a number <n then r=0;// to count the number of divisors number++; for (i=1 ; i<=number ; i++) { if ((number%i)==0) r++; } if(r==2)// The prime number is divided on 1 and on itself { printf(" %d \t",number); } } system("pause"); } |