Write a program to check if the entered number is a prim number or not using recursion in c.
In this post we shall learn through a program in c using recursion that the entered number is a prim number or not. The number will be entered by user and program display the out put that the number is prim or not.
Example:
Entered number = 13
Output = Number is prim.
Now see the program:
#include<stdio.h>
#include<conio.h>
int checkPrime(int);
int main()
{
int number,flag = 0;
printf("Enter number\t");
scanf("%d",&number);
if(number == 2)
{
printf("The number is prime\n");
}
else if(number < 2)
{
printf("The smallest prime number is 2\n");
}
else
{
flag = checkPrime(number);
if(flag)
{
printf("Number is prime\n");
}
else
{
printf("Not prime");
}
}
}
int checkPrime(int x)
{
static int n = 2, flag1 = 1;
if(n == x)
{
return 0;
}
else if(x%n == 0)
{
flag1 = 0;
n++;
}
else
{
n++;
flag1 = 1;
checkPrime(x);
}
return(flag1);
}
Output:
Enter number 29Number is prim.
Do not forget to subscribe me for latest update on my blog , follow me and grow your knowledge. thank you
0 comments:
Post a Comment