Write a c program to check if a given number is a prime or not
#include<stdio.h>
#include<math.h>
int isPrime(int number){
int i;
if (number < 2){
return 0;
}
if (number == 2 || number == 3){
return 1;
}
for (i = 2; i <= sqrt(number * 1.0); i++){
if (number % i == 0){
return 0;
}
}
return 1;
}
int main(){
int number;
printf("Enter number: ");
scanf("%d",&number);
if(isPrime(number)){
printf("The number is prime\n\n");
}else{
printf("The number is NOT prime\n\n");
}
scanf("%d",&number);
return 0;
}
Comments
Leave a comment