Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

code in C++ Assignment 6 (10 points): Prime Number Your goal is to ask the user

ID: 3732048 • Letter: C

Question

code in C++

Assignment 6 (10 points): Prime Number Your goal is to ask the user for a number and to determine if that number is a prime number. Use a function to determine if the number is prime. Your program should have the following: . The name of the program should be Assignment6. . Ask the user for an integer. Store this result in a variable with an appropriate name. 3 comment lines (description of the program, author, and date). (I point) Write a function that has an integer parameter and determines if that integer is a prime number. A prime number is divisible by itself and . This function should return true if the number is prime and false if the number is not prime. Give this function an appropriate name. (5 points) .Call the function and save the result in a variable. (2 points) Display the result of whether the user entered number is prime or not prime. (2 points)

Explanation / Answer

/*
This program checks whether a number is a prime number or not.
3/18/2016
*/

#include <iostream>
using namespace std;

int checkPrime(int);

int main()
{
int num;

cout << "Enter a number: ";
cin >> num;
bool isPrime = checkPrime(num);//function checkPrime is called
if(isPrime == true)
cout << endl<<num << " is a prime number.";
else
cout <<endl<<num << " is not a prime number.";
return 0;
}
int checkPrime(int n)
{
bool flag = true;
for(int i = 2; i <= n/2; ++i)//running for loop from 2 to half the input number
{
if(n%i == 0)
{
flag = false;
break;
}
}
return flag;
}

-----------------------------------------------------------

OUTPUT

----------------------------------------------------------