c program to find whether the given number is prime or not ?

>> Friday, October 28, 2011


In this C program, we will input the number when program runs and find whether that entered number is prime or not. This program uses "%" or "modulus" operator which gives the remainder of two numbers when it is divided. For example : 7 % 2 = 1, as 1 is remainder when 7 is divided by 2. So if the number is not prime then its % operation will give output '0' otherwise it is prime. Let us see how this program is implemented.
#include<stdio.h>                     // Including library
#include<conio.h>
int main()                              
{
     int n;
     int i;
     printf(" enter the number : ");   
     scanf("%d",&n);                  
     for(i=2; i<n; i++)
     {
              if(n%i==0)              
               {
                printf("%d is not a prime number.", n);
               getch();                                                  // hold  the  screen to display the above message
               return (1);                                              // return statement  will return the value and terimate
               }
     }
printf("%d is a prime number.", n);
getch();
}
         
Lets see the output of this program
c program to find prime number, output for prime number
output : Program to find prime number 



Here’s the another  program to find whether  the given number is prime or not by using recursion function.


0 comments:

Post a Comment

About This Blog

This blog will contain c programs related to interview preparation, basic programs, operating system, graphics, data structure, algorithms implementation, compiler and porjects. C is one of the most widely used programming languages of all time. This blog is intended for engineer students and for the people who are preparing for their interview in IT sector.

Share and Save