nested while loop

hi,im studeing c++ know so;
i have this question:



i want to out put the positive divisors of the integer in decreasing order.

I think that i should use 2 loops firstsheak the number will be
bigger than 0.the scend is to sheak the modulas (x%2==0) then y=x%2.

thanks alot>.<
Last edited on
By "the integer" I presume you mean a number given by the user?
The first loop would probably be to make sure that the number is acceptable - some kind of input control. The second loop would be to cycle down the divisors, checking each to see if it is actually a divisor of the given number. So what exactly are you asking?
I think this is what you might be looking for Just 1 while loop was necessary, although it was a little difficult figuring out what you were trying to achieve. Good Luck

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

#include <iostream> 
 using namespace std;   
int main()
  
{
     int a,b;
     
     cout << "enter numerator: ";
     
     cin >> a;
     b=a;
     while (b>=1) 
 
     {if(a%b==0) cout << a << " / " 

     << b << " = " << a/b << "\n";

    b--;}

return 0;

}





enter numerator: 12
12 / 12 = 1
12 / 6 = 2
12 / 4 = 3
12 / 3 = 4
12 / 2 = 6
12 / 1 = 12




Last edited on
yes this is it .
but how to make the output in adecresing order?
Um, do the loop the opposite way?
can i have some clarification ,what part u mean ?
the loop reversed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <iostream> 
 using namespace std;   
int main()
  
{
     int a,b;
     
     cout << "enter numerator: ";
     
     cin >> a;
     b=1;
     while (b<=a) 
 
     {if(a%b==0) cout << a << " / " 

     << b << " = " << a/b << "\n";

    b++;}

return 0;

}




  

enter numerator: 12


12 / 1 = 12
12 / 2 = 6
12 / 3 = 4
12 / 4 = 3
12 / 6 = 2
12 / 12 = 1



Topic archived. No new replies allowed.