Answer to Question #285145 in C++ for Abdullah

Question #285145

Write a C++ program which contains a function of bool type and takes an argument of integer type value. The argument is taken from user in main function and calls the bool function. The bool type function will check whether the argument number is a prime number or not. The result will be send back to main function and displays the status of the number


1
Expert's answer
2022-01-06T02:01:45-0500
#include <iostream>

bool is_prime (int number)
{
    for( int i = 2; i != number; ++i)
    {
        if(number % i == 0)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int number = 0;
    std::cout<<"Enter number: ";
    std::cin>>number;
    if(is_prime(number))
    {
        std::cout<<"Number is prime"<<std::endl;
    }
    else
    {
        std::cout<<"Number is not prime"<<std::endl;
    }
    
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog