C++ Looping Issue

Started by
11 comments, last by F E A R 18 years, 1 month ago
It should work (you'll have to try it to be sure).

You can even do better, because
a) number is constant in the inner loop
b) once you have executed the inner loop, total is set to the correct value

So in the end, you can simply do
  // 1 is allways a divisor of N  total = 1;  for (divisor = 2; divisor < number; divisor++)  {    remainder = number % divisor;    if (remainder == 0)    {      // divisor is a real divisor ? we add it to the total	      total += divisor;    }  }  // here, total is OK  if (total < number)  {    numberType = "Deficient";  }  else if (total == number)  {	    numberType = "Perfect";  }  else if (total > number)  {	    numberType = "Abundant";  }  cout << number << " is " << numberType << endl;


Now, I don't know what is your current level in C++, but it seems to me that this can be put in a function (with a parameter called "number"). The outer loop itself can be put in a function.

HTH,
Advertisement
Functions are after this lab. They're next on the list. :)

Thanks, I'll try some of this after I get back.
Everyone, I can't thank you enough. I was able to get it working this morning by swapping the "while" for an "if" and moving the couts outside the inner loop.

I'm learning--little by little. This experience helps, so thanks!

You all got great ratings from me, BTW.

This topic is closed to new replies.

Advertisement