Program to find first N
Prime Numbers
Below is a program to find first n
prime numbers using nested for
loops, where the value of n
is input by the user.
Before you continue with program, check this topics to understand the program :
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i = 3, count, c;
printf("\nEnter the number of prime numbers required : ");
scanf("%d", &n);
if(n >= 1)
{
printf("\n\nFirst %d prime numbers are : ", n);
printf("2 ");
}
// iteration for n prime numbers
// i is the number to be checked in each iteration starting from 3
for(count = 2; count <= n; i++)
{
// iteration to check c is prime or not
for(c = 2; c < i; c++)
{
if(i%c == 0)
break;
}
if(c == i) // c is prime
{
printf("%d ", i);
count++; // increment the count of prime numbers
}
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output: