Do while

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	int num = 2;
	do
	{
	
	num *= 2;
	
	}while (num < 48);
	
	cout << num << endl;
} 

I can't solve the abv code. Care to guide me?
One word: overflow. -- Nevermind.

What exactly is the problem you are having?
Last edited on
that looks fine to me,

int num =2;

2 * 2 = 4


4 * 4 = 16


16 * 16 = 256 or something

which is higher than 48, but because the do-while started as 16, the while evaluated as true... so it executes the formula . then prints

cout << 256 << endl;

then program ends....

there is no return statement however in int main().
remember to return 0;


EDIT:
i did however run this to check and the result was 64, not sure how that came about....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    int num=2;

    do
    {
        num *= 2;
    }
    while(num < 48);

    cout << num << endl;

    return 0;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    int num=2;

    do
    {
        num *= 2;
    }
    while(num < 48);

    cout << num << endl;

    return 0;

}



num = num * 2 (2 * 2 = 4)
num = num * 2 (4 * 2 = 8)
num = num * 2 (8 * 2 = 16)
num = num * 2 (16 * 2 = 32) 
num = num * 2 (32 * 2 = 64)

woops :P

*faceplants keyboard*
Last edited on
Topic archived. No new replies allowed.