help while loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/
int Num = 18;
cout << " Multiples of 9 from 27 to 81:\n";
while ( num <= 81 )
{
	Num = Num + 9;
	cout << Num << " ";
	num++;

}
int n = 1;
int NUM = 0;
cout << " 3 raised to the power of n + 1 where n is 1 to 10\n";
while ( n <= 10)
{
	3 * pow(n) + 1 = NUM;
	cout << NUM << " ";
	NUM++;
}
		
	return 0;
}




my program should produce the output

Multiples of 9 from 27 to 81:27 36 45 54 63 72 81
3 raised to the power of n + 1 where n is 1 to 10: 4 10 28 82 244 730 2188 6562 19684 59050


where did I go wrong?
Right then, now that I slapped myself awake.

I assume the pow you are using is the one included in cmath, it takes 2 arguments, in this case 3.0 and n+1.

You're also mixing up math with c++, NUM should be assigned the equation:
NUM = pow(3.0, n+1);

Finally you're incrementing NUM in the while loop, this also means that n is never changed and you'll have an infinite loop, so change that to n++ instead.
Last edited on
1
2
3
4
5
6
7
8
9
10
int n = 1;
int sum = 0;
cout << " 3n=1 where n is 1 to 10\n";
while ( n <= 10)
{
	sum = pow(3.0, n+1);
	cout << sum << " ";
	n++;
}
		


the math is still not right for the last output?
anyone help???????????????
1
2
3
4
5
6
7
8
9
int n = 1;
int sum = 0;
cout << " 3n=1 where n is 1 to 10\n";
while ( n <= 10)
{
	sum += pow(3.0, n+1); //note +
	cout << sum << " "; // maybe endl would make it look better?
	n++;
}
Last edited on
1
2
3
4
5
6
7
8
9
int n = 1;
int sum = 0;
cout << " 3n=1 where n is 1 to 10\n";
while ( n <= 10)
{
	sum = pow(3.0 , n + 1);
	cout << sum << " ";
	n++;
}

still my math is wrong I need it to ouput this: 3 raised to the power of n + 1 where n is 1 to 10: 4 10 28 82 244 730 2188 6562 19684 59050
1
2
3
4
5
6
7
8
9
int n = 1;
int sum = 0;
cout << " 3n=1 where n is 1 to 10\n";
while ( n <= 10)
{
        sum = pow(3,n)+1;
        cout << sum << " ";
        n++;
}


output is correct with this one, but i dont know why it is impressed that way...

true table of power 3 is: 3, 9, 27, 81.....
Last edited on
That fixed it thanks so much!!!!!!
Topic archived. No new replies allowed.