PDA

View Full Version : loop power help


shadowdogg
11-01-2007, 12:25 AM
Hi there,

I desperately need help on a program I need to do.

A user will enter a value and a power.

This will need to work out value^power.

Completely stuck - unsure how to work out the problem.

It is part of a debugging exercise and I have just been randomly changing the stuff to see if i can work out the answer and have failed miserably, spending 5 hours on this little problem.


This is the code i have been using and it is all wrong. Don't understand how i can get it to do num * pow, pow times.

Please give me some assistance in the direction I need.

Thanks.

int answer = num;
for(int i = 1; i < pow; i++) {
answer *= i;
System.out.println("The value of num inside the loop is " + num);
System.out.println("The value of pow inside the loop is " + pow);
}
System.out.println("The end value of answer is" + answer);
System.out.println("The end value of pow is" + pow);
System.out.println("The end value of num is" + num);
System.out.println(num + " to the power of " + pow + " is " + answer);

shyam
11-01-2007, 06:38 AM
if num=2 and pow=4 ur code is calculating

2 *= 1 (2)
2 *= 2 (4)
4 *= 3 (12)

then i=4 and no longer less than 4 and u get 12 as the answer

what u should be doing is

2 *= 2 (4)
4 *= 2 (8)
8 *= 2 (16)