Solution to A prime number is a number that is evenly divisible only by itself and 1. … - Sikademy
Author Image

Archangel Macsika

A prime number is a number that is evenly divisible only by itself and 1. For example, the number 5 is prime because it can be evenly divided only 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 C++ program using a function that 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 C++ program.

The Answer to the Question
is below this banner.

Can't find a solution anywhere?

NEED A FAST ANSWER TO ANY QUESTION OR ASSIGNMENT?

Get the Answers Now!

You will get a detailed answer to your question or assignment in the shortest time possible.

Here's the Solution to this Question


using namespace std; int is_prime(int n)  {   int i, Flag = 1;   if (n == 0 || n == 1) Flag=0;   else    {     for(i = 2; i <= n/2; ++i)  {       if(n % i == 0)  Flag = 0;     }   }   return (Flag); } int main()  {   int n=0;   while(n<=0)   {   cout<<"\n\tEnter a number (>0): "; cin>>n;   }   if(is_prime(n)==1) cout<<"\n\tThe number "<<n<<" is a PRIME NUmber";   else cout<<"\n\tThe number "<<n<<" is NOT a PRIME NUmber";   return(0); }

Related Answers

Was this answer helpful?

Join our Community to stay in the know

Get updates for similar and other helpful Answers

Question ID: mtid-3-stid-44-sqid-1061-qpid-57