# need help with my loops project

• 10-24-2007
j_breed
need help with my loops project
okay so im having trouble getting the right outputs for this program i am writing for my class.
its supposed to be taking numbers from the user and analyzing them for their divisors(factors)
and also whether they are prime or perfect numbers(i haven't gotten to this point yet).
the problem with the outputs is that it won't put out all the divisors or will put out doubles.

Code:

```#include <iostream> using namespace std; int main() {         int input,                 j, //number divding into the input number                 decider;         double divisor; //an output number                         char ans;                                do         {                                        do                 {        cout << "Please enter a number between 1 and 1000" << endl;                         cin >> input;                 }                 while (!(input >= 1) || !(input <= 1000));                 cout << "Number: " << input << endl;                 if (input != 1)                         cout << "Divisors: ";                 else                         cout << "This number is neither prime nor perfect" << endl;                   for (j = 1; (j <= input) && (input % j == 0); j++)                 {                                divisor = static_cast<float>(input)/j;                         if (divisor != j)                                 cout << divisor<< " ";                                                                                         if (j != divisor)                                 cout << j << " ";                 }                         cout << "\nWould you like to continue (Y/N)?\n";                 cout << "You must enter a Y or a N." << endl;                 cin >> ans;         }         while (((ans != 'n') && (ans != 'N')) || ((ans == 'Y') || (ans == 'y')));                         return 0; }```
• 10-24-2007
dwks
Code:

`for (j = 1; (j <= input) && (input &#37; j == 0); j++)`
In other words, this loop will stop as soon as input % j == 0. Actually, this will happen on the first test since input % 1 = 0. In other words, the loop will actually never execute.

Code:

```                        if (divisor != j)                                 cout << divisor<< " ";                                                                                         if (j != divisor)                                 cout << j << " ";```
If divisor != j, then j != divisor. Right?

Code:

`while (((ans != 'n') && (ans != 'N')) || ((ans == 'Y') || (ans == 'y')));`
Consider tolower() or toupper() from <cctype>.

I think you need to have another look at the problem. How would you do it on paper?
• 10-24-2007
j_breed
well the parameters are what we have been taught so far in this class so basically i have to use do while, while, and for loops, the loop does execute but with the way im coding it it just doesn't want to put out either all the divisors or only one iteration of a divisor(as in it will output 1 1 3 4 4)
• 10-25-2007
dwks
Quote:

well the parameters are what we have been taught so far in this class so basically i have to use do while, while, and for loops, the loop does execute but with the way im coding it it just doesn't want to put out either all the divisors or only one iteration of a divisor(as in it will output 1 1 3 4 4)
Yeah, I read your expression as the opposite of what it is.
Code:

`for (j = 1; (j <= input) && (input &#37; j == 0); j++)`
That loop stops as soon as it reaches a number that is not a divisor. For example, if you enter 100, it will loop with j=1 and find 100 and 1 as divisors; then it will loop with j=2 and find 50 and 2; and then when j=3, input % j != 0, and the loop exits, missing some divisors of 100 including 20 and 25.

That's not what you want. You don't want the loop to stop executing as soon as it finds a number that isn't a divisor. You want the loop to keep going and simply not print anything in this case.

BTW, if you get it working, as written, you'll find every divisor twice. And you won't find a divisor if that divisor is the square root of the number.

Like I said, think about how you would do this on paper.