Sunday, May 8, 2011

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

/* Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. */


#include <stdio.h>


void main()

{

int no,counter,counter1,check;

clrscr();

printf(“<———————–PRIME NO. SERIES————————>”);

printf(“\n\n\n\t\t\tINPUT THE VALUE OF N: “);

scanf(“%d”,&no);

printf(“\n\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();

}

No comments:

Post a Comment