-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathPerfectionCalculator.cpp
More file actions
95 lines (95 loc) · 2.53 KB
/
PerfectionCalculator.cpp
File metadata and controls
95 lines (95 loc) · 2.53 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
#include <iostream>
using namespace std;
int main()
{
//Is VS Are variables
int less = 0; //Same as lower - For is (VS) are
//---------------------------------------------------------------
//Declare variables
int user_input; // User entered number
int lower = 0; //Represents all #'s lower than user input
int type_lower = 0; //Represents all #'s lower than user input - Used to find type of number
int perfect_storage = 0; //Determines whether number is perfect, abundant, or deficient
int zero = 0; //Zero
int num_props = 0; //Number of proper factors
//---------------------------------------------------------------
//Get number input
do
{
cout << "Enter in a positive integer: ";
cin >> user_input;
if (user_input <= 0)
{
cout << "That isn't a positive integer. " << endl;
}
} while (user_input <= 0);
//---------------------------------------------------------------
//Is vs Are
for (less = user_input - 1; less > zero; less -= 1)
{
if (user_input % less == 0)
{
num_props += 1;
}
}
//---------------------------------------------------------------
//Print out proper factors answer
if (num_props == 1)
{
cout << "The proper factor of " << user_input << " is ";
}
else
{
cout << "The proper factors of " << user_input << " are: ";
}
//---------------------------------------------------------------
//Reset variables
num_props = 0;
//---------------------------------------------------------------
//Get proper factors of number - print out factors
for (lower = user_input - 1; lower > zero; lower -= 1)
{
if (user_input % lower == 0)
{
if (lower == 1 && num_props + 1 > 1)
{
cout << " and ";
}
cout << lower;
num_props += 1;
if (lower == 3 && user_input % 2 != 0)
{
cout << "";
}
else if (lower > 2)
{
cout << ", ";
}
}
}
//---------------------------------------------------------------
//Print out number of proper factors
cout << endl << user_input << " has " << "(" << num_props << ")" << " proper factors.";
cout << endl;
//---------------------------------------------------------------
//Get whether the number is perfect, abundant, or deficient
for (type_lower = user_input - 1; type_lower > zero; type_lower -= 1)
{
if (user_input % type_lower == 0)
{
perfect_storage += type_lower;
}
}
if (perfect_storage == user_input)
{
cout << user_input << " is a perfect number.";
}
else if (perfect_storage > user_input)
{
cout << user_input << " is an abundant number.";
}
else
{
cout << user_input << " is a deficient number.";
}
}