A prime number is a number that can be evenly divided by onl

A prime number is a number that can be evenly divided by only itself and 1. For example, the number 5 is prime because it can be evenly divided by only 1 and 5. The number 6, however, is not prime because it can be evenly divided by 1, 2, 3, and 6. Write a Boolean method named is Prime that takes an integer as an argument and returns true if the argument is a prime number or false otherwise. Use the method in an application that lets the user enter a number and then displays a message indicating whether the number is prime.

Solution

The program in c is as follows:

#include<stdio.h>
#include <stdbool.h> //include this directory in your program for boolean function declaration

main()
{
int n, result;

printf(\"Enter an integer to check whether it is prime or not.\ \");
scanf(\"%d\",&num);

result = check_prime(num);
//applying check for the returned result
if ( result == 1 )
printf(\"prime.\ \");
else
printf(\" not prime.\ \");

return 0;
}
//boolean function to check if the number is prime or not
bool prime(int num)
{
int i;

for ( i = 2 ; i <= num - 1 ; i++ )
{
//if the number is divisible by any of the number then it is not boolean hence return false
if ( num%i == 0 )
return false;
}
//if the number is not divisible by any number from the loop then counter i reaches till the num hence it is prime
if ( i == num )
return true;
}

 A prime number is a number that can be evenly divided by only itself and 1. For example, the number 5 is prime because it can be evenly divided by only 1 and 5

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site