C PROGRAM TO GENERATE 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();
}

ilook website

4 Comments

  1. have not really tested your program because i wrote some myself what am so concerned about is the compilation time and the processing time when a user in put large n,don’t know if you have consider that

  2. how to check whether a no. can be written in the form of power of 2…..like 32 as 2^5…….8 as 2^3….but 20 cannot

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.