ISourceCode

Make the frequent cases fast and the rare case correct

Program to find prime number upto a given number

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: