I lost my 1 while looping....

This is a discussion on I lost my 1 while looping.... within the C++ Programming forums, part of the General Programming Boards category; I am finding numbers that the sum of its divisors is either less than (deficient) equal to (perfect) or greater ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    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;
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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.)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    3
    JaWiB
    Thank you for your response.

    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).

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    3

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 07:03 AM
  2. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 08:13 PM
  3. Lost again looping
    By dAzed in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2004, 10:55 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 12:01 AM
  5. Help me understand For Looping
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2002, 12:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21