# Thread: using for loop to calculate power (ex... x^4)

1. Member
Join Date
Oct 2014
Posts
1
Rep Power
0

## using for loop to calculate power (ex... x^4)

i'm new to java and i'm trying to figure out how to calculate power in java using the for loop.

the geometric sequence is (d*c^(n-1)).

so it goes d, d*c^1, d*c^2,......., d*c^(n-1)

??????for (i=1; i<=n; i++){
result
}????????
please help, i don't understand how to program this. how do i write this using for loop?

2. ## Re: using for loop to calculate power (ex... x^4)

Suppose you know the value x^(n-1) and you are asked for the value x^n; take the previous value and multiply by x: x^(n-1)*x == x^n; as you should know, x^0 == 1. This is what it looks like in code:

Java Code:
```double result= 1;
for (int i= 0; i < n; i++)
result*= x;```
kind regards,

Jos

3. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
28

## Re: using for loop to calculate power (ex... x^4)

Is that c^(n-1), or (d*c)^(n-1)?

Either way it doesn't really matter in terms of how to calculate the power using a loop, it just changes the start value.

So how would you do it by hand, for a value 'x' and a value of 'n'? Forget about code for the minute.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•