1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
// Define function for output
void showResults(int sequence, int prime) {
cout << "Prime #" << setw(4) << left << sequence << right <<" = " << prime << endl;
return;
}
int primeFunction() {
int n=1, status = 1, num = 3, count, c, prime = 2;
for (count = 2; count <= n; ) //loop that will iterate through n numbers
{
for (c = 2; c <= (int)sqrt(num); c++)// for each element check whether it is prime or not
{
//if number is completely divisible by a number other than 1 and itselft,then number is not prime
if (num%c == 0)
{
status = 0;
break;
}
}
//if it is a prime number, print it
if (status != 0)
{
//cout << prime << endl;
count++;
}
status = 1;
num++;
}
return prime;
}
ofstream outfile;
//void saveResults(int sequence, int prime) {
// outfile << "Prime #" << setw(4) << left << sequence << right << " = " << prime << endl;
// return;
//}
int main()
{
// Set data types
int num = 1; // , int s = 1, int p = 1;
int sequence = 1;
int prime = 1;
// Get information from user
cout << "How many prime numbers would you like to see? (0 to quit)" << endl;
cout << endl;
cout << "Please enter a whole number between 30 and 2000 > ";
cin >> num;
while (num == 0) // Allowing for user to quit program
{
cout << endl;
cout << "Quitting program " << endl;
cout << endl;
system("pause");
return 0;
}
while (cin.fail() || num < 30 || num > 2000) // Ensure amount entered is within acceptable range
{
cout << "Please enter a whole number between 30 and 2000 > ";
cin >> num;
cin.clear();
cin.ignore(1000, '\n');
while (num == 0) {
cout << endl;
cout << "Quitting program " << endl;
cout << endl;
system("pause");
return 0;
}
}
cout << endl;
cout << endl;
// Display confirmation message that input is valid and program is ready to proceed
cout << "Thank you. \nProcessing your results." << endl;
cout << endl;
// Clear the screen to prepare for fresh output
system("pause");
system("CLS");
cout << "The first 20 prime numbers are as follows" << endl;
cout << endl;
for (int x = 1; x <= 20; x++) {
showResults(sequence++, prime);
}
cout << endl;
cout << endl;
cout << "Please see primes.txt for your full results." << endl;
// Save all N prime numbers to a file named “primes.txt”.
//ofstream outfile;
//outfile.open("primes.txt", ios::out);
//for (int x = 1; x <= num; x++) {
// //saveResults(sequence++, prime);
// outfile << "Prime #" << setw(4) << left << sequence << right << " = " << prime << endl;
// sequence++;
// }
//outfile.close();
system("pause");
return 0;
}
|