The below code checks whether the entered number is prime or not.
Logic: Prime numbers are defined as the numbers which are only divisible by one or itself.
This can be checked by dividing the number from 2 to that number.If the number is divisible on any other number other then itself then that number will not be a primer number.Also as 1 is not a prime number,if the user enters 1 print not prime.
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int x,y,count=0;
printf("Enter a number:");
scanf("%d",&x);
y=2;
if(x==1)
printf("The number %d is not prime",x);
while(y<=x)
{
if(x%y==0)
count++;
y++;
}
if(count==1)
printf("The number %d is prime",x);
else
printf("The number %d is not prime",x);
getch();
}
Logic: Prime numbers are defined as the numbers which are only divisible by one or itself.
This can be checked by dividing the number from 2 to that number.If the number is divisible on any other number other then itself then that number will not be a primer number.Also as 1 is not a prime number,if the user enters 1 print not prime.
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int x,y,count=0;
printf("Enter a number:");
scanf("%d",&x);
y=2;
if(x==1)
printf("The number %d is not prime",x);
while(y<=x)
{
if(x%y==0)
count++;
y++;
}
if(count==1)
printf("The number %d is prime",x);
else
printf("The number %d is not prime",x);
getch();
}
Post a Comment