# I lost my 1 while looping....

• 05-15-2006
mkimbrell
I lost my 1 while looping....
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; }```
• 05-15-2006
JaWiB
First of all, that's got to be some of the ugliest formatting I've ever seen. At least add some indentation, for example you might do one of the following:
Code:

```if (/*...*/) {   //code } //or for (/*...*/) {   //code }```
Is MAX supposed to be the number you're finding the factors of? It looks like you're trying to find the factors of n, but you set n equal to MIN, which doesn't seem to make sense. And I don't think you need nested loops (unless I'm completely misunderstanding what you're trying to do.)
• 05-16-2006
mkimbrell
JaWiB

Yes the formatting is off because I was trying to make it easier for someone to read on here. I moved everything to left and shrunk everything... Should have just left it alone.
Sorry it was hard to read.
Anyway, The MIN is the minimum constraint of the outer loop and MAX is the maximum constraint of the outer loop. So if MIN =1 and MAX = 100, the program with loop all integers till it reaches 100 .

The user can input whaterver MIN or MAX they wish and the program should output the all the integers from min to max with their associated sum of the divisors and it's orientation (deficient, perfect, or abundant).
• 05-16-2006
JaWiB
Ok, I think I understand better now. It seems to me you don't need these "special cases." Maybe you can just output '1' before the inner loop (without checking if n equals 1)

Also, in the inner loop, shouldn't the condition be div < n (or maybe n/2), not div < MAX?
• 05-17-2006
mkimbrell
Thank you
Thank you for your help. I found my mistake, I should have simply seperated the functions and tidied up the program a little. It works now. Thank you.