This is a variation of programming challenge 6.22 described in Gaddis on page 37
ID: 3802272 • Letter: T
Question
This is a variation of programming challenge 6.22 described in Gaddis on page 373 Write a program that determines if an entered integer is prime. 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 is Prime, 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. TIP: Recall that the % operator divides one number by another, and returns the remainder of the division. In an expression such as num1 % num2, the % operator will return 0 if num1 is evenly divisible by num2. Do not accept a value that is less then one for the number to be tested. Your program must also include comments with your name, the date, and a short description of the project. It must also print this information as a splash screen at the beginning of the program run. Your source code must be named project_7_abc.cpp where the abc are your own initials. Submit only the source code file - the one that ends in .cpp - in the class dropbox on box.psu.edu by 11:59 pm on March 22, 2017.Explanation / Answer
#include<iostream>
using namespace std;
bool checkPrime(int n)
{
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
return false;
}
return true;
}
int main()
{
int n=2;
do
{
if(n<=1)
{
cout<<"Number should be greater than 1 ";
}
cout<<"Enter number: ";
cin>>n;
}while(n<=1);
bool isPrime = checkPrime(n);
if(isPrime)
cout<<"true"<<endl;
else
cout<<"false"<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.