COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
perfect.cpp
Go to the documentation of this file.
1/*
2 This program prints out the perfect numbers between 1 and N
3*/
4
5#include <iostream>
6using namespace std;
7
8bool isPerfect(int j){
9 int sum_factor = 0;
10 for (int factor = 1; factor<j; factor++){
11 if (j % factor == 0) {
12 sum_factor += factor;
13 }
14 }
15 return j == sum_factor;
16}
17
18const int N = 1000;
19
20int main()
21{
22 cout << "For the integers from 1 to " << N << ":\n";
23
24 for (int j = 2; j <= N; ++j)
25 if (isPerfect(j))
26 cout << j << " is perfect\n";
27
28 cout << endl;
29
30 return 0;
31}
bool isPerfect(int j)
Definition perfect.cpp:8
const int N
Definition perfect.cpp:18
int main()
Definition perfect.cpp:20