division for a counter??? help please!

Hey yall, this is part of a big assignment I have coming up and this part has put my progress at a dead stop.

The question itself is: "Create an integer var9 and set it to 100. Create a loop structure where var9 counts down the iterations by halving the counter each time. The moment that it reaches 0 or less, it exits. Print the values of var9 with each pass."

This code is crucial for the next part of the assignment as well.
I attached what code I came up with, and while it does sorta-kinda work, I know there is a better way. Thanks in advance


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

int var9 = 100;

        do{
            int var1 = 0;
            int var2 = 0;
            int var3 = 0;
            int var4 = 0;
            int var5 = 0;
            int var6 = 0;
            int var7 = 0;

                var1 = (var9 / 2);
                    cout << var1;
                    cin.get();

                var2 = (var1 / 2);
                    cout << var2;
                    cin.get();

                var3 = (var2 / 2);
                    cout << var3;
                    cin.get();

                var4 = (var3 / 2);
                    cout << var4;
                    cin.get();

                var5 = (var4 / 2);
                    cout << var5;
                    cin.get();

                var6 = (var5 / 2);
                    cout << var6;
                    cin.get();

                var7 = (var6 / 2);
                    cout << var7;
                    cin.get();

        }while (var7 > 0);
int var9{100};
for(; var9; var9/=2) //for (initialize does nothing here ; var9 != 0 is the same as var9, divide by 2 each pass)
cout << var9 << endl;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main() 
{
    // Create an integer var9 and set it to 100.
    auto var9 = 100;

    //Create a loop structure where var9 counts 
    // down the iterations by halving the counter each time.
    do
    {
        //Print the values of var9 with each pass.
        std::cout << var9 << '\n';

    }while ((var9 /= 2) > 0); //The moment that it reaches 0 or less, it exits

    return 0;
}
100
50
25
12
6
3
1
Last edited on
Topic archived. No new replies allowed.