The main criteria to decide if a number is prime can be done by using

if(i%j==0). if the condition is satisfied that means there exists a number other than itself than can divide the number perfectly. if i == j condition is satisfied it makes sure we are dividing a number by itself , which is acceptable for the prime number property.

#include<stdio.h>
void main()
{
int i,j,n;
printf(" Enter the number till where prime numbers are listed: \n");
scanf("%d",&n);
printf("\n");
for(i=2;i<=n;i++)
{
for(j=2;j<=i-1;j++)
if(i%j==0)
break;
if(i==j)
printf("%d\t",i);
}
/*Continue till n numbers*/
}

OUTPUT:

laptop:~$ ./a.out

Enter the number upto which we have to find the prime number:

10

2 3 5 7

### Like this:

Like Loading...

*Related*