Prime Number Program In C Using Recursion

In this article, we are going to write a Prime Number Program In C Using Recursion.

To make this program, we will use the following concept given below.

If you do not know about these topics well, then you may not understand this program, so you should know about them in detail from the link given above.

Now without wasting time let’s start programming.

Prime Number Program In C Using Recursion
Prime Number Program In C Using Recursion

C Program To Find Prime Number Using Recursion

Algorithm

  • Program Start
  • Declare Variables
  • Input A Number to check prime or not
  • Calling Recursive Function
  • Check conditions
  • Check conditions and return result
  • Recursive Function End
  • Check Condition and Display Result
  • Program End

Program

//Prime Number Program In C Using Recursion

#include<stdio.h>

int PrimeorNot(int, int);
void main()
{
    //variable declaration
    int num, prime;

    //input number
    printf("Enter a positive number to check Prime or Not: ");
    scanf("%d", &num);

    //calling function
    prime = PrimeorNot(num, num/2);

    //checking condition and display result
    if(prime == 1)
    {
        printf("%d is a prime number\n", num);
    }
    else
    {
        printf("%d is a not a prime number\n", num);
    }
}
//recursive Function
int PrimeorNot(int n, int i)
{
    if(i == 1)
        return 1;
    else
    {
        if(n%i == 0)
            return 0;
        else
            PrimeorNot(n, i-1);
    }
}

Output

Enter a positive number to check Prime or Not: 11
11 is a prime number

Conclusion

So friends, in this article, we have written a Prime Number Program In C Using Recursion

If you have difficulty to understand any program or have any doubts or questions, then tell me in the comment below.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.