The intent of this assignment is to expose you to writing a program that makes u
ID: 645549 • Letter: T
Question
The intent of this assignment is to expose you to writing a program that makes use of a function. Toward this end, you will write a program that inputs and only accepts positive integers and will determine if the number entered by the user is a prime number. You are to write a function, named isPrime, that determines if the integer is a prime number and returns a Boolean value indicating if the number is prime (True) or not prime (False).
BACKGROUND
Recall that a prime number is a number this is only divisible by 1 and the value itself. Prime numbers have been found to be useful in encryption. One use in public-key cryptography is to use the multiple of 2 prime values to use to encrypt a message or data.
Special Note: The number 1 is not considered a prime number. Your function should return False if sent the number 1.
REQUIREMENTS
Write and implement a command-line C++ program that meets the following requirements:
1.Name the program file PrimeFinder.cpp . Create the main() function within this file. Additionally create a function named isPrime within this same file. Place the function below the main function which requires a function prototype at the top of the file.
2.Prompt the user for a positive, non-zero integer value.
3.Validate all inputs by asking the user to re-enter invalid data. Only accept integers larger than 0.
4.Call your function (isPrime) to determine if the number is a prime number and let the user know the status.
5.Prompt the user if they would like to perform another test. If the user responds with
Explanation / Answer
please rate - thanks
sorry the picture of the output is not uploading properly
C:UsersEileenDesktop>PrimeFinder 30
prime numbers between 2 and 30
2 3 5 7 11 13 17 19 23 29
Do it again? (y/n): n
C:UsersEileenDesktop>PrimeFinder 1
prime numbers up to 1
Do it again? (y/n): y
enter a max (positive) integer to find the primes: 30
prime numbers up to 30
2 3 5 7 11 13 17 19 23 29
Do it again? (y/n): y
enter a max (positive) integer to find the primes: -3
must be an integer > 0
enter a max (positive) integer to find the primes: 50
prime numbers up to 50
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47
Do it again? (y/n): n
----------------------------------------
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
bool isPrime(int);
int main(int argc, char * argv[])
{int number,n=0,maxx;
bool prime,first=true;
char again;
do
{if(first)
{maxx=atoi(argv[1]);
first=false;
}
else
{
cout<<"enter a max (positive) integer to find the primes: ";
cin>>maxx;
}
while(maxx<=0)
{cout<<"must be an integer > 0 ";
cout<<"enter a max (positive) integer to find the primes: ";
cin>>maxx;
}
n=0;
cout<<"prime numbers up to "<<maxx<<" ";
for(number=1;number<=maxx;number++)
{if(isPrime(number))
{cout<<setw(6)<<number;
n++;
if(n==10)
{n=0;
cout<<endl;
}
}
}
cout<<" Do it again? (y/n): ";
cin>>again;
}while(again=='Y'||again=='y');
cout<<endl;
}
/*find prime numbers
get the number checking for prime
return true if the number is fine
false otherwise
*/
bool isPrime(int n)
{int i;
if(n==1)
return false;
for(i=2;i<=sqrt(n);i++) //mathematically only have to check to the sqry of a number
if(n%i==0) //if you find a factor the number isn't prime
return false;
return true; // get here only if no factors
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.