To print the prime numbers using C


Levels of difficulty: / perform operation:

C program to print the prime numbers within a given number.

C Program

#include <stdio.h>
#include <conio.h>
main()
{
	int n,i,j,c;
	clrscr();
	printf("\nENTER A NUMBER: ");
	scanf("%d",&n);
	printf("\nPRIME NUMBERS WITHIN %d\ ARE:\n",n);
	for(i=1;i<=n;i++)
	{
		c=0;
		for(j=1;j<=i;j++)
		{
			if(i%j==0)
				c++;
		}
		if(c==2)
			printf("%d ",i);
	}
	getch();
}


Output