Repository navigation
Expand file tree
/
Copy pathProgram 3.cpp
More file actions
105 lines (87 loc) · 1.96 KB
/
Copy pathProgram 3.cpp
File metadata and controls
105 lines (87 loc) · 1.96 KB
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
#include <iostream>
using namespace std;
bool IsPrime(int num);
bool IsPerfect(int num);
int MenuOption();
int main()
{
// Jingnan Qi Assignment 3
int primeUpper,
perfectUpper,
countPrime = 0,
countPerfect = 0;
// if user selected Option 1 or 2
while (countPrime >= 0) {
switch (MenuOption()) {
// if user selected Option 1
case 1:
cout << "What is the upper limit for the values in the list? ";
cin >> primeUpper;
for (int i = 1; i <= primeUpper; i++)
if (IsPrime(i)) {
cout << i
<< "\t";
countPrime += 1;
if (countPrime % 5 == 0)
cout << "\n";
}
break;
// if user selected Option 2
case 2:
cout << "What is the upper limit for the values in the list? ";
cin >> perfectUpper;
for (int j = 1; j <= perfectUpper; j++)
if (IsPerfect(j)) {
cout << j
<< "\t";
countPerfect += 1;
if (countPerfect % 5 == 0)
cout << "\n";
}
break;
// if user selected Option 3
case 3:
cout << "Thank you for using this program\n" << endl;
}
}
return 0;
}
int MenuOption()
{
int inValue;
do {
// obtain option value from user
cout << "\nPlease select from the following options:\n"
<< "\n\t1:\tList prime numbers"
<< "\n\t2:\tList perfect numbers"
<< "\n\t3:\tTerminate the program\n"
<< "\nOption? ";
cin >> inValue;
} while (inValue != 1 && inValue != 2 && inValue != 3);
return inValue;
}
bool IsPrime(int num)
{
bool result = true;
if (num < 2)
return false;
else
for (int divisor = 2; divisor <= num / 2 && result != false; divisor++)
if (num % divisor == 0)
result = false;
return result;
}
bool IsPerfect(int num)
{
bool result = true;
int total = 0;
if (num < 2)
return false;
else
for (int divisor = 1; divisor < num; divisor++)
if (num % divisor == 0)
total += divisor;
if (total != num)
result = false;
return result;
}