Untitled
1 week ago in Plain Text
/*
Ohad Elias
ID 307880880
Computer science Introduction
exerscise 5 question 2

this program prints the perfect numbers
up to the user input
and than prints the perfect numbers until 500
*/

#include <iostream>
using namespace std;

int perfect(int num)
{
int sum = 0;
for (int i = 2; i < num; i++)
{
if (num % i == 0) // so i divides num
sum = sum + i; // we want to sum all the numbers that divides num
}
sum = sum++; // for num itself
bool equal;
sum == num;
if (sum == num)
equal = true;
else
equal = false;
return equal;
}

void printPerfects(int num=500) // 500 if the output is empty
{
for (int n = 2; n < num; n++)
{
if (perfect(n) == true)
cout << n << " ";
}
cout << endl; // we want an empty line, even if the program didnt print anything at all
return;
}

int main()
{
cout << "enter a number:\n";
int num;
cin >> num;

while (num < 1) // num should be positive
{
cout << "ERROR" << endl;
cin >> num;
}

printPerfects(num);
printPerfects();

return 0;
}

/*
enter a number:
8200
6 28 496 8128
6 28 496

C:\Users\elias\Documents\ìéîåãéí\ùðä à\îáåà ìîãòé äîçùá\ùéòåøé áéú\úøâéì áéú 5\question 2\Debug\question 2.exe (process 26272) exited with code 0.
Press any key to close this window . . .

*/
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