Ask Question

6.21: isPrime Function 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.

+1
Answers (1)
  1. 7 August, 14:22
    0
    The program to this question can be given as:

    program:

    #include / /define header file.

    using namespace std;

    int isPrime (int number); / /declare function.

    int isPrime (int number) / /function definitation.

    {

    int i;

    if (number<1) / /conditional statements.

    return false;

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

    {

    return true;

    }

    else

    {

    for (i=2; i
    {

    if (number%i==0)

    return false;

    }

    return true;

    }

    }

    int main () / /main method.

    {

    int number=0; / /declare variable.

    cout << "Enter a number for check it is prime number or not : "; / /print message.

    cin >> number; / /input by user.

    if (isPrime (number) = =true) / /check condition.

    cout << number << " is prime.";

    else

    cout << number << " is NOT prime.";

    return 0;

    }

    Output:

    Enter a number for check it is prime number or not : 3

    3 is prime.

    Explanation:

    In this program we define a function that is (isPrime ()). In this function, we pass the user input as a parameter. Then we use conditional statement in these statements we check the number is divisible by itself and 1. If the number is divisible it returns true otherwise it returns false. In the last we define the main function in that function we take user input and pass it in the function, and print its return value.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “6.21: isPrime Function A prime number is an integer that is greater than 1 and that is only evenly divisible by itself and 1. For example, ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers