c program to find whether the given number is prime or not using Recursion ?
>> Friday, October 28, 2011
This C program uses recursion for finding whether the entered number is prime or not. A function is called recursive if the statement in a body calls the same functions. It uses the modulus operator ('%') to find the prime number.Lets have a look on this program. Function name is prime in this program and it calls prime() again till the condition is satisfied and till the parser is not return to the main function.
#include<stdio.h> #include<conio.h> int prime(int n) { static int i=2; if(n%i==0&&n!=2) { return(1); } if(i<n) { i++; prime(n); // recursion call } } int main() { int p; printf("enter the number to check for prime number : "); scanf("%d",&p); int l=prime(p); //function returns the value and puts in 'l' variable if(l==1) //conditions printf("%d is not a prime number ", p); else printf("%d is a prime number ", p); getch(); }
Here is output for the above program :
output : Find prime number using recursion |
0 comments:
Post a Comment