Results 1 to 5 of 5
  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default 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. #2
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    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. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

    [edit]

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

  4. #4
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    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. #5
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    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;
    }

Similar Threads

  1. I'm falling in love with java...
    By IndioDoido in forum Introductions
    Replies: 2
    Last Post: 08-30-2009, 03:38 AM
  2. What does rep power mean in our site?
    By makpandian in forum New To Java
    Replies: 12
    Last Post: 06-27-2009, 05:42 PM
  3. Power of Java
    By jeffranc in forum JDBC
    Replies: 5
    Last Post: 09-05-2008, 07:14 AM
  4. Power*MatchMaker 0.9.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 08:21 PM

Posting Permissions

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