Untitled
3 years 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 . . .
*/