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

• 10-15-2010, 05:32 AM
Danieldcc
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;
}
}
• 10-15-2010, 06:00 AM
Danieldcc
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;
}
}
• 10-15-2010, 06:13 AM
pbrockway2
Quote:

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.
• 10-16-2010, 04:34 AM
Danieldcc
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
• 10-16-2010, 04:38 AM
Danieldcc
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;
}