Java hw help

Discussion in 'Java' started by cvillejin, Mar 21, 2011.

  1. cvillejin

    cvillejin New Member

    Joined:
    Mar 21, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I'm new to Java and I have no idea how to do this problem. I don't know what loops are or how to use them.

    5. In mathematics, exponentiation of an is a multiplied by itself n times. For instance, 53 is 5*5*5, or 125. Define a method so it may be called using the following statement and finds the correct exponential operation:
    [FONT=&quot]int result = exponent(5,3);[/FONT]
    You must use a loop in your method. Using the java.lang.math method pow() is not allowed. You can assume that the exponent is a positive, whole number. Note that the ^ operator does not perform exponentiation.
     
  2. cvillejin

    cvillejin New Member

    Joined:
    Mar 21, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    you can call the method what you want but I'm going to call it expo for now

    public int expo(int n, int x){

    int z, y;

    y=n;
    for(z=0: z<x; z++){
    y=y*n;
    }
    return y;
    }


    is this correct?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice