Results 1 to 5 of 5
- 10-15-2010, 05:32 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 83
- 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;
}
}
- 10-15-2010, 06:00 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 83
- 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;
}
}
- 10-15-2010, 06:13 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
So, what output does it give?still doesn't give me the output i'm looking for
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.
[edit]
power=power*(base-1) had more going for it: at least it kept accumulating.
- 10-16-2010, 04:34 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 83
- 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
- 10-16-2010, 04:38 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Similar Threads
-
I'm falling in love with java...
By IndioDoido in forum IntroductionsReplies: 2Last Post: 08-30-2009, 03:38 AM -
What does rep power mean in our site?
By makpandian in forum New To JavaReplies: 12Last Post: 06-27-2009, 05:42 PM -
Power of Java
By jeffranc in forum JDBCReplies: 5Last Post: 09-05-2008, 07:14 AM -
Power*MatchMaker 0.9.2
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-27-2007, 08:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks