Answer to Question #301606 in C for Silas

Question #301606

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


1
Expert's answer
2022-02-23T06:24:04-0500


#include<stdio.h>
#include<math.h>


int isPrime(int regNumber){
	int i;
	if (regNumber < 2){
		return 0;
	}
	if (regNumber == 2 || regNumber == 3){
		return 1;
	}
	for (i = 2; i <= sqrt(regNumber * 1.0); i++){
		if (regNumber % 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