Print all prime numbers from 1 to N

C program to print all prime numbers from 1 to N.

Prime number is a number that is greater than 1 and is divisible only by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. The first few prime numbers are {2, 3, 5, 7, 11, 13, 17 ...}

Program

#include<stdio.h>
#include<conio.h>
void main()
{
	int i,j,flag,n;

	printf("Enter the value of N\t:");
	scanf("%d",&n);

	printf("\nPrime numbers between 1 and %d are\t:",n);

	for(i=2;i<=n;i++)
	{
		flag=0;
		for(j=2;j<=i/2;j++)
		{
			if(i%j==0)
			{
				flag=1;
				break;
			}
		}
		if(flag==0)
		{
			printf("%d\t,",i);
		}
	}

	getch();
}

Output

Enter the value of N    :65

Prime numbers between 1 and 65 are      :2      ,3      ,5      ,7      ,11     ,13     ,17     ,19     ,23     ,29
,31     ,37     ,41     ,43     ,47     ,53     ,59     ,61     ,