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

C++ Concepts tested by this program: 1.Learn to organize code within a function

ID: 3689458 • Letter: C

Question

C++

Concepts tested by this program:

1.Learn to organize code within a function

2.Learn to pass data to and return Boolean data from function

3.Use of loop

4.Use file processing Program Prime Numbers

A prime number is an integer that is greater than 1 and 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 isPrime, 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 that reads in an integer that is less than 3001 stores a list of all the prime numbers from 2 through that number in a file named "PrimeList.txt" and to the standard output.

-Primelist.txt file contents bellow:

2

3

5

7

11

13

17

19

23

Prime numbers written to Primelist.txt.

-Summary of what program has to do:

1. Ask the user "Insert integer number less than 3001."

2. If number is more than 3001 then a error should appear, if it is less, the number insterted will be there and also it will read the "PrimeList.txt" file which contains a list of numbers.

Screenshot: basically number 23 was inserted and the rest came from.txt

X CAUserslOwner Documents Visual Studio 20131Projects ConsoleApplication21DebuglConsoleAp... CMSC 140 CRN xxxx Project 5: Prime Numbers *Written by a student Bill Morgan 04/03/2016 lould you please enter an integer number that should be less than 3001 23 2 19 23 Prime numbers written to PrimeList.txt Press any key to continue - . -

Explanation / Answer

#include <iostream>
#include <fstream>
using namespace std;
bool isPrime(int);
int main()
{
int num=0;
int i;
bool prime;
ofstream outFile;
outFile.open("PrimeList.txt");
while (num == 0 )
{
cin >> num;
}
for(i=2;i<num;i++)
if(isPrime(i))
outFile << i << " ";
cout << "Prime numbers written to PrimeList.txt. ";
outFile.close();
return 0;
}
bool isPrime(int n)
{
int i;
for(i=2;i<n-1;i++)
if(n%i==0)
return false;
return true;
}

#include <iostream>
using namespace std;
bool isPrime (int num);
int main ()
{
int num=0;
cout << "Enter a number and whether it is prime: ";
cin >> num;
if (isPrime(num)==true)
cout << num << " is prime.";
else
cout << num << " is NOT prime.";
return 0;
}
bool isPrime(int input)
{
if(input<1)
return false;
else if (input == 1||input ==2 ||input==3)
{
return true;
}
else
{
for(int i=2; i<input; i++)
{
if(input%i==0)
return false;
}
return true;
}
}