182k views
1 vote
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. 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 by 1, 2, 3, and 6. Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise.

User Lil
by
4.4k points

2 Answers

0 votes

Answer:

Since no programming language is stated, python will by used.

Step-by-step explanation:

def isPrime():

#first we ask for a number that will be tested

data= int(input("please enter a number"))

#the next line is where the number will be tested to see if its a prime

if (data>1):

if(data % 2)==1:

print("true")

else:

print("False")

else:

print("Enter a number greater than 1")

isPrime()

User Miuosh
by
4.8k points
4 votes

Answer:

I am writing a C++ program.

bool isPrime(int integer) //boolean function that takes integer argument //and returns true if argument is prime number otherwise false

{ if (integer > 1) // if the integer value is greater than 1

{ for (int i = 2; i <= integer; ++i)

//loop starts from 2 and iterates until the value of i gets greater than integer //value

{ if (integer % i == 0) // if integer value is completely divisible by i

{ if(integer == i) // if integer value is equal to i

return true; //function returns true

else //if integer value is not divisible by i

return false; //function returns false } } } } }

Step-by-step explanation:

In order to see the output of the program you can write a main() function as following:

int main() { //start of the main() function body

int integer; // int type variable integer

cout<< "Enter an integer: "; //prompts user to enter an integer value

cin >> integer; //reads the input from user

cout << integer<<" "; //prints the integer value entered by user

if (isPrime(integer))

// calls boolean function to check if the integer value is prime number or not

{ cout << " is a Prime number." << endl; }

//displays the message number is prime

else //if the integer value is not prime

cout << " is not a Prime number." << endl; } //displays message

The output along with the program is attached as screenshot.

A prime number is an integer greater than 1 that is evenly divisible by only 1 and-example-1
A prime number is an integer greater than 1 that is evenly divisible by only 1 and-example-2
User Matt Burrow
by
4.3k points