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>
#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();
}
{
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
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