Print all Prime numbers between 1 to n
Sample Output
Print all Prime numbers between 1 to n. Enter a number: 20 Prime numbers are: 2 3 5 7 11 13 17 19
Source Code
#include<stdio.h>
int main()
{
int i, j, num;
printf("Print all Prime numbers between 1 to n.\n\n");
printf("Enter a number: ");
scanf("%d", &num);
printf("Prime numbers are:\n");
for(i = 1; i <= num; i++)
{
int isPrime = 1;
for(j = 2; j < i; j++)
{
if(i % j == 0)
{
isPrime = 0;
}
}
if(isPrime == 1)
{
if(i == 1)
{
/**
* 1 is not a prime number
*/
}
else
{
printf("\n%d\n", i);
}
}
}
return 0;
}
Sample Output
Print all Prime numbers between 1 to n. Enter a number: 11 Prime numbers are: 2 3 5 7 11
No comments:
Post a Comment