Create a program c++ A prime number is a number that is only evenly divisible by
ID: 3572297 • Letter: C
Question
Create a program c++
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.
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 by calling it in the main() function with numbers from 1 to 100.
[When you have a nested loop problem, converting the inner loop to a subprogram might be a
good idea]
Explanation / Answer
Here is the C++ Code for the above given scenario:
#include <iostream>
using namespace Prime_number;
bool isPrime (int num);
int main ()
{
int num=0;
cout << "Enter a number and I'll tell you whether it is prime: ";
cin >> num;
if (isPrime(num)==true)
cout << num << " is prime.";
else
cout << num << " is NOT prime.";
return 0;
}
bool isPrime(int input)
{
if(input<1)
return false;
else if (input == 1||input ==2 ||input==3)
{
return true;
}
else
{
for(int i=2; i<input; i++)
{
if(input%i==0)
return false;
}
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.