C++ PLEASE DO NOT COPY AND PASTE A PROGRAM FOUND ONLINE AS AN ASWER. I know I ca
ID: 3805180 • Letter: C
Question
C++ PLEASE DO NOT COPY AND PASTE A PROGRAM FOUND ONLINE AS AN ASWER. I know I can find C++ codes all over the internet to do this, but I am looking for someone who can help me write AND understand a C++ code to perform this, and please write coments explaining what each line of code is doing, It will help me understand how the code flows and works. Thank you!
A prime number is an integer greater than 1 that can only be divided evenly by itself or 1. Examples of prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ... Numbers that are not prime are: Any even number, 9 since it is divisible by 3, multiples of 5, etc.
Note: You must use several functions in the solution of this problem ( main does not count as one of your solutions). Please note the number one is not a prime number by definition. Global variables are not allowed. main does not count as one of your functions.
Write a program that will accept from the user a positive integer (make sure you validate the number is positive this would make a good function candidate.) and determine if the number entered by the user is a prime number. Notify the user if the number is prime or it is not prime. Your program should also display the number entered by the user.
The naive or brute-force approach is to check if it is divisible by any number between 1 and itself. Use the following algorithm to determine if a number is prime or not.
If the number is even and greater than 2, it is not prime.
Moreover, you should only test the number against integers less than or equal to the square root of its own value.
Explanation / Answer
// C++ code to check if input number is prime
#include <iostream>
#include <cstdlib>
#include <math.h> // for sqrt
using namespace std;
// function to check if number is prime or not
bool checkPrime(int n)
{
// If the number is less than or equal to 1 it is not prime.
if(n <= 1)
return false;
// If the number is even and greater than 2, it is not prime.
else if(n%2 == 0)
return false;
else
{
// since we have check for 1 and 2, starting i with 3
// testing the number against integers less than or equal to the square root of its own value.
for (int i = 3; i <= sqrt(n); ++i)
{
// check if n is divisible by any number in range 3 to sqrt(i)
if(n%i == 0)
return false;
}
}
// if all the test pass and false is not returned, means number is prime
return true;
}
int main()
{
int n;
// while loop which takes input n and checks if number is prime or not
while(true)
{
// input n
cout << " Enter a number(-1 to exit): ";
cin >> n;
// break the loop if n is negative
if(n < 0)
{
cout << "Negative Number found, Exiting code"
break;
}
// call the function and check if n is prime
if(checkPrime(n) == true)
cout << n << " is a prime number ";
else
cout << n << " is NOT a prime number ";
}
return 0;
}
/*
output:
Enter a number(-1 to exit): 10
10 is NOT a prime number
Enter a number(-1 to exit): 9
9 is NOT a prime number
Enter a number(-1 to exit): 8
8 is NOT a prime number
Enter a number(-1 to exit): 13
13 is a prime number
Enter a number(-1 to exit): 4
4 is NOT a prime number
Enter a number(-1 to exit): 34
34 is NOT a prime number
Enter a number(-1 to exit): 31
31 is a prime number
Enter a number(-1 to exit): -1
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.