NextIndexPrevious

Programs in C language

12. 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("Enter a value");
scanf("%d",&no);

for(counter = 1; counter <= no; counter++)
{
 check = 0;
 for(counter1 = counter-1; counter1 > 1 ; counter1--)
   if(counter%counter1 == 0)
   {
    check++;
    break;
   }
   if(check == 0)
   printf("%d\t",counter);
}
getch();
}
Input :
20
Output :
2 3 5 7 11 13 17 19
Father of C Language
Dennis Ritchie

Dennis Ritchie

Born: September 9, 1941, Bronxville
Died: October 9, 2011, Murray Hill
Google+