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 :

C program to find whether the entered number is prime or not using recursion
output : Find prime number using recursion


0 comments:

Post a Comment

About This Blog

This blog will contain c programs related to interview preparation, basic programs, operating system, graphics, data structure, algorithms implementation, compiler and porjects. C is one of the most widely used programming languages of all time. This blog is intended for engineer students and for the people who are preparing for their interview in IT sector.

Share and Save