Answer to Question #307612 in C for Silas

Question #307612

Write a c program to check if a given number is a prime or not


1
Expert's answer
2022-03-08T09:38:59-0500


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS