Program A1 Finding Prime Numbers This program asks you to determine a set of pri
ID: 3821397 • Letter: P
Question
Program A1 Finding Prime Numbers This program asks you to determine a set of prime numbers, given some input fram the user. Your program should ask the user enter an integer between land 100. For purposes of discussion, let's call that number nun. If numiseutside that range, your pregramshauld print out an error message and re prompt for another number (ie-have aninput validation loop) Your program should then calculate the first num prime numbers and print them both to the screen to an output file. Primeout tart. Your output should be neatly sormaned. 10 numbers Perline, and should look like the following examples. Forthe fina cuample. Ict's 20. The first 20 prines 13 37 43 53 59 or ifnum The first 25 prines 11 13 17 19 cs1336 Programming Fundamentals Page: 2 Here we are using a 5 byte field just as we didina previous assignment. Since the 100th prime is a three digit mumber, that should work well. (Other spacing's are obviously possible just make sure there are 10 numbers per line.) To solve this problem, your program should haveafunction called isPrime O that ukes an integer as an argument and retums true ifthe argument isprime or false otherwise. This is a boolean returming function as in the book in Section 69) The prolotype the function should be bool is Prime (int number Using this function can significantly mplif your main procesing loop. useisaperfect example offenenowaldecomposition, also called modwar programming. lamadular we move the solution of a specific task this case determining number is prime or not to a function and then use that fanction to create the overall solution to problem. Functional decomposition is an eweellent programming technique and hould be used in all of your programs from now on. (Note: an example of boolean retuming function and how to use it is given en p.333 of the book. There the bookeannetuming function is and it is called inside an if statement. You can use your isPrine lint) function in the same way) For this problem, it is particularly important develop your pseudooode you start programming Your pseudocode for the nain function might begin as follows: correctly. that number henreen Jand inclarive Set count Ser curr taun While count number Endy End While Close PrimeOvrant You should developpseudocode for the isPrime (int function as well.Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
bool isPrime(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n%2 == 0 || n%3 == 0) return false;
for (int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;
return true;
}
int main()
{
int num;
while(true)
{
cout << "Enter a number in range 1-100: ";
cin >> num;
if (num >= 1 && num <= 100)
{
break;
}
else
{
cout << "Please enter numbe rin given range only." << endl;
}
}
int count = 0;
int i = 2;
ofstream outfile("PrimeOut.txt");
while(count <= num)
{
if (isPrime(i))
{
cout << setw(2)<< i << " ";
outfile << setw(2)<< i << " ";
count++;
if (count %10 == 0)
{
cout << endl;
outfile << endl;
}
}
i++;
}
cout << endl;
outfile << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.