C program to print prime numbers.

0 CK 41 · February 23, 2015
Can anyone explain me the C program to print n prime numbers? I tried looking up on some websites but really couldn't understand how it works, here it is:


#include<stdio.h>
 
int main()
{
int n, i = 3, count, c;
 
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
 
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
 
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
 
return 0;
}


for ( count = 2 ; count

Post a Reply

Replies

Oldest  Newest  Rating
0 CK 41 · February 23, 2015
Here the complete code:

#include<stdio.h>
 
int main()
{
int n, i = 3, count, c;
 
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
 
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
 
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
 
return 0;
}
0 Mathias Frits Rørvik · February 23, 2015
This code is incomplete.
  • 1

C

107,258 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator