Write a program to generate list of prim number up to n, where n being entered by user.
In this post we shall learn how to generate prim number series using recursion in c language. the limit of series shall be entered by user i.e. n number series of prim number.
Example :
Now see the program:
#include<stdio.h>
#include<conio.h>
void primeSeries(int);
int main()
{
int limit;
printf("Enter the limit of prime number series\t");
scanf("%d",&limit);
printf("\n\t2");
primeSeries(limit);
}
void primeSeries(int n)
{
static int a = 3;
int i,flag = 0;
if(n == 1)
{
printf("\nSeries ends here");
}
else
{
for(i = 2; i <= a-1; i++)
{
if(a%i != 0)
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
if(flag)
{
printf("\n\t%d",a);
a++;
primeSeries(n - 1);
}
else
{
a++;
primeSeries( n);
}
}
}
Output:
Enter the limit of prime number series 72
3
5
7
11
13
17
Series ends here
Do not forget to subscribe me for latest update on my blog , follow me and grow your knowledge. thank you