C Program to Check whether a Number is Prime or Not using Recursion

This is a C program to find whether a number is prime or not using recursion.

Problem Description

The following C program, using recursion, finds whether the entered number is a prime number or not.

Problem Solution

A prime number is an integer that has no integral factor but itself and 1.

Program/Source Code

Here is the source code of the C program to find an element in a linked list. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to find whether a Number is Prime or Not using Recursion
 */
#include <stdio.h>
 
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);
       }       
    }
}
Program Explanation

In this C program, we are reading the integer number using ‘num’ variable. A prime number is an integer that has no integral factor but itself and 1. The check variable is used to call the primeno() function by passing the value of ‘num’ variable and the value of division of ‘num’ variable value by 2 as an argument.

advertisement

The primeno() function is used to find whether the entered number is a prime number or not. If else condition statement is used to check the value of ‘i’ variable is equal to 1 and return the value of ‘i’ variable to the called variable ‘check’.

Otherwise, if the condition is false execute the else statement and call the primeno() function by passing the value of ‘num’ variable and the decrement the value of ‘i’ variable by 1. Return the resulted value to the called variable ‘check’.

If else condition statement is used to check that the value of ‘check’ variable is equal to 1. If the condition is true print the statement as prime number. Otherwise, if the condition is false print the statement as not a prime number.

Free 30-Day Python Certification Bootcamp is Live. Join Now!
Runtime Test Cases
 
$ cc pgm24.c
$ a.out
Enter a number: 456
456 is not a prime number
 
$ a.out
Enter a number: 89
89 is a prime number

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.