C++ Concepts tested by this program : 1.Learn to organize code within a function
ID: 3688257 • 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.
Screenshot:
Title, Prompts, Output, Labels:
Program will write output to the file and to the standard output.
1. A program writes the following text to the standard output(Can bee seen in the top of the screen shot):
CMSC 140 CRN <your course CRN> Project 5: Prime Numbers
2. After closing the file, the program writes to the standard output:
a. on a line by itself :
Prime numbers are written to PrimeList.txt
b. a list of primes
Input Validation If the value read in exceeds 3000, the program writes on a line by itself in standard output:
Your input integer should be less than 3001. Try again.
Porgram should look something like this: In the images the user inserted 23 and the rest of the numbers came froma seperate .txt file that u have to make containing those numbers(pretty sure).
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 <fstream>
#include <iostream>
#include <string>
using namespace std;
bool isPrime(int number)
{
int i;
for (i=2; i<number; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
int main()
{
ofstream outfile;
outfile.open("PrimeList.txt");
int num;
cout << "Enter an integer that is less than 3001:"<<endl;
cin >> num;
while(!(num<3001)){
cout << "Integer must be less than 3001, try again:"<<endl;
cin >> num;
}
cout << "CMSC 140 CRN <your course CRN> Project 5: Prime Numbers" << endl;
for(int i=2; i<=num; i++){
if(isPrime(i)){
cout << i << endl;
outfile << i << endl;
}
}
outfile.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.