64 747
Assignments Done
99,3%
Successfully Done
In September 2018

Answer to Question #45936 in C++ for Enrique

Question #45936
calculations.
A number is called perfect if the sum
of its divisors is equal to the original number. A number is
called deficient if the sum of its divisors is less than the
original number. A number is called abundant if the sum
of its divisors is more than the original number. Write a
program that answers the following questions: What
numbers below 5000 are perfect? What odd numbers
below 5000 are abundant? What are the relative
proportions of deficient, abundant, and perfect numbers?
5000 should be in a global constant.
Expert's answer
#include <iostream>
using namespace std;
const int MAX_NUM = 5000;
int sum_of_divisors(int n) {
int sum = 0;
for (int i = 1; i < n; ++i) {
if (n % i == 0) sum += i;
}
return sum;
}
int main() {
int perfect = 0, deficient = 0, abundant = 0, odd_abundant = 0;
for (int i = 1; i <= MAX_NUM; ++i) {
int sum = sum_of_divisors(i);
if (i == sum) {
cout << "Number " << i << " is perfect" << endl;
++perfect;
}
else if (i < sum) {
cout << "Number " << i << " is abundant" << endl;
++abundant;
}
else {
cout << "Number " << i << " is deficient" << endl;
++deficient;
}
if ((i < sum) && (i % 2 == 1)) {
cout << "Number " << i << " is odd abundant" << endl;
++odd_abundant;
}
}
cout << "A number of deficient, odd_abundant, abundant, perfect: "
<< deficient << ":" << odd_abundant << ":" << abundant << ":" << perfect << endl;
cout << "Relative proportion of deficient, odd_abundant, abundant, perfect: "
<< (float) deficient/(float) MAX_NUM << ":" << (float) odd_abundant/(float) MAX_NUM << ":" << (float) abundant/(float) MAX_NUM << ":" << (float) perfect/(float) MAX_NUM << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
24.09.14, 12:37

Dear visitor,
Please check the updated answer.

ricky
15.09.14, 22:06

When you run this program it only gives you 1 perfect number and I know there is more than one perfect number . How do you fix it ?

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions