Pages

Write a program to check given number is prime or not using recursion

Wednesday, 14 May 2014

#include <stdio.h>
#include <conio.h>

int isPrime(int number, int i)
{

    if(i==1)
    {
        return 1;
    }
    else
    {
       if(number%i==0)
         return 0;
       else
         isPrime(number,i-1);
    }
}

int main(){

    int number, prime;

    printf("Enter a positive number: ");
    scanf("%d", &number);

    prime = isPrime(number, number/2);

   if(prime==1)
        printf("%d is a prime number.", number);
   else
      printf("%d is not a prime number.", number);

   getch();
}

 

Search Box

Most Reading

Contact Form

Name

Email *

Message *