Prime number program in c using recursion




C code to check a number is prime number or not by recursion:

#include<stdio.h>

int isPrime(int,int);

int main(){

    int num,prime;

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

    prime = isPrime(num,num/2);

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

   return 0;
}

int isPrime(int num,int i){

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

Sample output:

Enter a positive number: 13
13 is a prime number

C code to check a number is prime number or not without recursion:

#include<stdio.h>

int isPrime(int);

int main(){

    int num,prime;

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

    prime = isPrime(num);

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

   return 0;
}

int isPrime(int num){

    int i=2;

    while(i<=num/2){
         if(num%i==0)
             return 0;
         else
             i++;
    }

    return 1;
}





Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples

12 comments:

Anonymous said...

for better program u can divide till square root of number.
while(i<=Math.sqrt(num))

mun said...

wap to c using for loop check number is prime or not.

Anonymous said...

Dude good collection, but improve complexity of the program as well.

Anonymous said...

tanku

Krrish Jaya said...

Hi,
The following code is working correctly with recursive function for find out prime or not.
char isPrime(int);

int main(){

int num;
char prime;

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

prime = isPrime(num);
printf("\nPrime=%d\n",prime);

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

return 0;
}

char isPrime(int num){

static int i=2;
if(i<=num/2)
{
if(num%i==0)
{
printf("In \"not prime loop\" i=%d,num=%d\n",i,num);
return 0;
}
else
{
i++;
printf("In \"prime loop\" i=%d,num=%d\n",i,num);
if(!(isPrime(num))){
printf("ISprime fun exit at i=%d,num=%d\n",i,num);
return 0;}

}
}

return 1;

Thnks,

Krish

Anonymous said...

thanx alot..:)

Anonymous said...

Learn basic of c programming from Programmingcampus

Anonymous said...

what is the use of static int?

Jayesh Panchal said...


Best way to find prime numbers in C | Sieve Of Erasthothanese method to generate prime numbers |

Anonymous said...

Check out programming video tutorials on this YOUTUBE Channel

Anonymous said...

The following is the correct C code to check a number is prime number or not by recursion
#include
#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);
}
getch();
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);
}
}
}

Karthick JAI said...

#include
#include
int i=2,x=1;
void dd(int u)
{
if(i<u)
{
if(u%i==0)
x=x*0;
else
x=x*1;
i++;
dd(u);
}
}
void main()
{
int n;
clrscr();
printf("enter ");
scanf("%d",&n);
dd(n);
if(x==0)
printf("not");
else
printf("pr");
getch();
}