C Program to Check Number is Prime or Not Using Recursion

Problem: Write a program to check whether the number is prime or not using recursion.

C program to check prime number using recursion

/* C Program to find whether a Number is Prime or Not using Recursion */

#include 

int primeno(int, int);

int main()
{

    int num, check;

    printf("Enter a number: ");

    scanf("%d", &num);

    check = primeno(num, num / 2);

    if (check == 1)

    {

        printf("%d is a prime number\n", num);

    }

    else

    {

        printf("%d is not a prime number\n", num);

    }

    return 0;

}

 

int primeno(int num, int i)

{

    if (i == 1)

    {

        return 1;

    }

    else

    {

       if (num % i == 0)

       {

         return 0;

       }

       else

       {

         return primeno(num, i - 1);

       }       

    }

}

/* Output of above code:-

$ a.out
Enter a number: 789
789 is not a prime number
 
$ a.out
Enter a number: 751
751 is a prime number

*/