I am finding numbers that the sum of its divisors is either less than (deficient) equal to (perfect) or greater than (abundent) to the number. The numbers have a min and max range. I have discovered that when I use min = 1 the program works without a hitch.. (to my knowledege) But, when I use a min greater than 1 the program looses it's 1 as a divisor. Does someone have an idea how to make 1 always a factor no matter what the minimum value is? Here is my code:
Code:#include <iostream> #include <iomanip> using namespace std; int main () { cout<< "What is the Minimum value? \n"; int MIN; cin >> MIN; cout<< "What is the Maximum value? \n"; int MAX; cin >> MAX; int n, div, sum; n = MIN; cout << "Number Factors sum classification" << endl;//Column Header for(n; n < MAX; ++n) //Outer loop - steps through integers { sum = 0; //Initialize sum cout << setw(4) << n << " "; //Row header if (n == 1) //Special case for n == 1 {cout << "1"; sum = 1; } else //Otherwise {for (div = MIN; div < MAX; ++div) //Inner loop - steps through divisors {if (div == 1) //If the divisor equals 1 - special case {cout << "1"; //Outputs the first factor sum = 1; //Adds the factor to the sum } else if (n % div == 0) //Otherwise, checks if integer n is divisible evenly { //If true if (n != div) //Checks to exclude the integer itself {cout << ", " << div; //Outputs the next factor sum += div; //Adds the factor to the sum } } } //End inner loop } cout <<sum; if(sum == n) //If the sum of the divisors equals the number {cout << " Perfect" << endl; //Output "Perfect" } else if (sum < n) {cout << " Deficient" << endl; //Output "Abundant" } else {cout << " Abundant" << endl; } } return 0; }



LinkBack URL
About LinkBacks


