R4R
Right Place For Right Person TM
 
R4R C C ExamplesAccept a number and check is it prime number or not a prime number
previous

Home

Next

Learn C with example

 

 Accept a number and check is it prime number or not a prime number

In this example we are going to accept a number from keyboard then find is it prime number or not?

Here we are taking number from keyboard .Then iterate for loop from 1 to t (number). In for loop check modulo of t with i if it is zero then increase c by one. When iteration  is completed then check c==2 then the number is prime else it is not prime. Here we are checking so because a prime number is divisible by 1 or itself. So c=2.If it is greater than two then it is not prime.

/* accept a number and check it  is prime number 
or not a prime number */

#include<stdio.h>
#include<conio.h>
void main()
{
int t,c=0,i;
clrscr();
printf("Enter a number\t");
scanf("%d",&t);
for(i=1;i<=t;i++)
{
if(t%i==0)
c++;
}
if(c==2)
printf("\nNumber is prime");
else
printf("\nNumber is not prime");
getch();
}

Output Of Example

Enter a number 23

Number is prime
previous

Home

Next

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R