Saturday 23 April 2011

Write a C program to generate all the prime numbers between 1 and n.



#include <stdio.h>
void main()
{
int no,counter,counter1,check;
clrscr();
printf("PRIME NO. SERIES");
printf("\nINPUT THE VALUE OF N: ");
scanf("%d",&no);
printf("\nTHE PRIME NO. SERIES B/W 1 TO %d : \n\n",no);
for(counter = 1; counter <= no; counter++)
{
check = 0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for(counter1 = counter-1; counter1 > 1 ; counter1--)
if(counter%counter1 == 0)
{
check++; // INCREMENT CHECK IF NO. IS NOT A PRIME NO.
break;
}
if(check == 0)
printf("%d\t",counter);
}
getch();
}

PRIME NO. SERIES
INPUT THE VALUE OF N: 25
THE PRIME NO. SERIES B/W 1 TO 25 :
1 2 3 5 7 11 13 17 19 23

No comments:

Post a Comment