C Program to Check Whether a Number is Prime or Not

Write a Program to check Prime or Not in C


Program in C to Check Prime Number from all the natural number given. 

#include <stdio.h>
int prime(int);
int natural(int n)           \\ Natural number function
 {
      
        if  (n==1)
            {     return 1;}
        natural(n-1);
        prime(n);
 }
 int   prime(int    n) {           \\ prime number function
       int   i,flag=0;
       for(i=2;i<=n/2;i++)
        {
             if(n%i==0)
              {
                  flag=1;
                  break;
              }
        }
        if(n==1)
         {
              printf("\n%d is Neither Prime nor Composite",n);
         }
         else{  
                 if(flag==0)
                 {
                    printf("\n%d  is Prime. ",n);
                 }
                 else 
                  {
                      printf("\n%d  Non Prime",n);
                  }
         }
        
 }
  
int main()
{
    int n=25;          \\ check prime number until 25.
     natural(n);        \\ call function natural()

    return 0;
}
 
  
 

Output :

                                                                                                                                               
2  is Prime.                                                                                                                                   
3  is Prime.                                                                                                                                   
4  Non Prime                                                                                                                                   
5  is Prime.                                                                                                                                   
6  Non Prime                                                                                                                                   
7  is Prime.                                                                                                                                   
8  Non Prime                                                                                                                                   
9  Non Prime                                                                                                                                   
10  Non Prime                                                                                                                                  
11  is Prime.                                                                                                                                  
12  Non Prime                                                                                                                                  
13  is Prime.                                                                                                                                  
14  Non Prime                                                                                                                                  
15  Non Prime                                                                                                                                  
16  Non Prime                                                                                                                                  
17  is Prime.                                                                                                                                  
18  Non Prime                                                                                                                                  
19  is Prime.                                                                                                                                  
20  Non Prime                                                                                                                                  
21  Non Prime                                                                                                                                  
22  Non Prime                                                                                                                                  
23  is Prime.                                                                                                                                  
24  Non Prime                                                                                                                                  
25  Non Prime              

Write a Program in C to Check Given Number is Prime or Not.

Program to Check Prime Number taking input from user

#include <stdio.h>
int prime(int);

 int   prime(int    n) {
       int   i,flag=0;
       for(i=2;i<=n/2;i++)
        {
             if(n%i==0)
              {
                  flag=1;
                  break;
              }
        }
        if(n==1)
         {
              printf("\n%d is Neither Prime nor Composite",n);
         }
         else{  
                 if(flag==0)
                 {
                    printf("\n%d  is Prime. ",n);
                 }
                 else 
                  {
                      printf("\n%d  Non Prime",n);
                  }
         }
        
 }
  
int main()
{
    int n;
    printf("Enter an integer: ");
    scanf("%d", &n);  
    printf("Number = %d",n);
   
     prime(n);

    return 0;
}
 

Output :


Enter an integer: 5                                                                                                                          
Number = 5                                                                                                                                   
5  is Prime.
  

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top