183k views
3 votes
A prime number is a number 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. Design a Boolean function called isPrime, that accepts an integer as an argument and returns True if the argument is a prime number, or False otherwise. Use the function in a program that prompts the user to enter a number and then displays a message indicating whether the number is prime. The following modules should be written: getNumber, that accepts a Ref to an integer, prompts the user to enter a number, and accepts that input isPrime, that accepts an integer as an argument and returns True if the argument is a prime number, or False otherwise showPrime, that accepts an integer as an argument , calls isPrime, and displays a message indicating whether the number is prime The main module, that will call getNumber and showPrime

1 Answer

4 votes

Answer:

// here is code in C++.

#include <bits/stdc++.h>

using namespace std;

// function to check prime

bool isPrime(int n)

{

// if input is less than 1

if(n<1)

return false;

// if input is 1,2 or 3

else if (n == 1||n ==2 ||n==3)

{

return true;

}

// if input is greater than 3

else

{

for(int i=2; i<n; i++)

{

if(n%i==0)

return false;

}

return true;

}

}

// driver function

int main()

{

// variable

int num;

cout << "Enter a number: ";

// read the input number

cin >> num;

// call function to check prime

if (isPrime(num)==true)

// if prime

cout << num << " is prime.";

else

// if not prime

cout << num << " is not prime.";

return 0;

}

Step-by-step explanation:

Read a number from user and assign it to "num".Then call the function isPrime() with "num" as parameter.In this function, if the input is less than 1 then it will return "false".If input is 1 or 2 or 3 then it will return "true".For an input greater than 3, if the number is divisible by only 1 and itself then function will return "true" otherwise it will return "false".

Output:

Enter a number: 17

17 is prime.

User Overburn
by
5.6k points