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
#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;
}
Comments
Leave a comment