I am writing a program to find the prime numbers in C using recursion. Here is the program I've written.
#include<stdio.h>
#include<conio.h>
void rec(int, int);
int main()
{
rec(2,2);
getch();
return 0;
}
void rec(int n, int x)
{
if(x>999)
return;
if(n==x)
{
printf("%d ,", x);
rec(2,x+1);
return;
}
if(x%n==0)
{
rec(2,x+1);
return;
}
rec(n+1,x);
}
I don't know what is wrong in it, it is working well till 887 by crashes after it. To check, just replace x>999
by x>300
, it will work, but not for x>999
. Please tell the fault in the program instead of writing a whole new program.