Q:

Prime Number Program In C Using Recursion

belongs to collection: Recursion Programs In C

0

write a Prime Number Program In C Using Recursion.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Factorial of a Number In C Using Recursion...