# Thread: computes and returns the n:th falling power of base

1. Member
Join Date
Sep 2010
Posts
84
Rep Power
0

## computes and returns the n:th falling power of base

This is one of my questions on my assignment and i have a hard time getting the solution. some assistance please. The question and my code is posted below.

#

int fallingPower(int base, int n) that computes and returns the n:th falling power of base, which is the product of n consecutive terms (base)*(base-1)*(base-2)* … * (base-n+1). For example, fallingPower(10,3) should return 720, which is 10*9*8.

public int fallingPower(int base, int n){

public int power;
for(int i = n; i > 0; i--){
power = power * (base - 1);
}
System.out.println(base);
return base;
}
}

2. Member
Join Date
Sep 2010
Posts
84
Rep Power
0
saw the couple little mistakes and corrected. it looks like this now but still doesn't give me the output i'm looking for

public int fallingPower(int base, int n){
power = base;

for(int i = n; i > 0; i--){
power = base * (base - 1);
}
System.out.println(power);
return power;
}
}

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
still doesn't give me the output i'm looking for
So, what output does it give?

One way to deal with this sort of problem is to include lots of System.out.println(). At least enough to know what numbers you are multiplying together.

So, "System.out.println("Starting with " + power)" before the loop and "Setting power to <whatever>" each time around the loop.

power=power*(base-1) had more going for it: at least it kept accumulating.

4. Member
Join Date
Sep 2010
Posts
84
Rep Power
0
ok. i'm still not getting the right output.

int fallingPower(int base, int n) {
int power = 1;
for(int i = n; i > 0; i--)
{
power = base * (base - 1);
}

return power;
}

when i enter (10,3), i get the result of 90 but it should be 720. Anyone can help me? i'm stuck on this for quite a while. Any help is appreciated

5. Member
Join Date
Sep 2010
Posts
84
Rep Power
0
never mind, i think i got it.....

int fallingPower(int base, int n) {
int power = 1;
for(int i = n; i > 0; i--)
{

power = power * base;
base--; }

return power;
}

#### Posting Permissions

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