A prime number is an integer that is greater than 1 and that is only evenly divi
ID: 653578 • Letter: A
Question
A prime number is an integer that is greater than 1 and that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6.
Write a function name isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the function in a complete program that prompts for a number and indicates whether or not it is prime.
Explanation / Answer
/* C program to check whether a number is prime or not. */
#include <stdio.h>
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
isPrime(n);
}
int isPrime(int n){
int i, flag=0;
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.