I have written the following code to check if a number is prime. I know that there are other, maybe better ways of doing i, but I just want to know what is wrong with this method. It is correctly identifying whether the number is prime or not, but in case i is prime, it is giving a segmentation error.
The code:
#include<stdio.h>
void prime(int);
int main()
{
int a;
printf("Enter number\n");
scanf("%d",&a);
prime(a);
return 0;
}
void prime(int a)
{
static int k=2;
if((a%k==0)&&(a!=k))
printf("Not a prime\n");
else
{
k++;
if(a==k)
printf("Prime\n");
prime(a);
}
}
Note: On trying to revise the code, I found that it makes no difference if you use a==k or k==a-1 in the else condition.