Skip to content

Write a C Program to check if the number is prime number or not

by yashan on August 31st, 2011

Write a program in the C programming language to check if the inputted number is a prime number or not. In this program for loop is used to create numbers from 2 to n-1 to check if they are divisible by n or not and if any of them is divisible then the control is transferred outside the for loop by using the break statement and the result is printed in negative. Below is the code of the program -

/* C Program to check if number is prime number or not */

#include<stdio.h>
void main()
{
     int n,i,ct=0,rd=0;

     printf("Enter any number \n");
     scanf("%d",&n);

     for(i=2;i<n;i++)
     {
             rd=n%i;
             if(rd==0)
             {
                     ct=1;
                     break;
             }
     }

     if(ct==0)
     {
            printf("\n %d is prime number",n);
     }
     else
     {
            printf("\n %d is not prime number",n);
     }

     getch();
}

Input – 23
Output – 23 is a prime number

9 Comments
  1. I WANT OUT PUT THIS PROGRAME

  2. molay permalink

    pls do in your pc

  3. u missed conio.h header file to use getch()

  4. chanda permalink

    here i have a question plz.can we do it without using loop?i mean by using relational operators

  5. Wat is ‘ct’ in this program …plss reply fast

  6. The holiday happens to coincide with the end-of-the-season for
    many of the Island’s young summer employees and brings on a last chance for partying hearty on Mackinac Island. People will say just about anything to get you out to their property to see a horse, in the hopes of selling you something else they might have. Morgan is a breed of horse very much desired by the horse community as they entertain all riding disciplines.

  7. Ramachandiran permalink

    I want to calculation explaination

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS