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;}
LATEST TUTORIALS
APPROVED BY CLIENTS