Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Beginner problem- Integer based exponentiation

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Beginner problem- Integer based exponentiation

    Hello everyone,

    I have an assignment to basically recreate what Math.pow does but with integers through the use of a while or for loop.

    My code when run multiplies and then saves the number. (I.E. 2^2=4, but 2^3= 2*2=4 and 4 would get saved so the last calculation would be 4*4=16).

    The following is the code I believe the problem is in the while loop, as I don't know how to make it so it doesn't save the problem of the first calculation:

     
    import java.util.*;
    public class IntPower
    {
        public static void main(String[] args)
        {
            int base=0;
            int power=0;
            int result=0;
            base=IntegerPower(base, power, result);
            System.out.println("The result of the calculation is: "+base);
        }
        public static int IntegerPower(int base, int power, int result)
        {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter the base: ");
            base=input.nextInt();
            System.out.println("Enter the exponent: ");
            power=input.nextInt();
            int count=1;
            while(count<power)
            {
               base*=base;
               count++;
            }
     
            return base;
        }
    }
    Last edited by wolfyt; March 6th, 2014 at 12:23 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Beginner problem- Integer based exponentiation

    why are parameters passed to the method? Their values are immediately changed by the method.
    The prompts for the input values for the method should be done OUTSIDE of the method and passed to the method as args.

    Get the values for the method before calling the method.
    Pass the values as args to the method which then uses them to compute the result and return it.

    The code should build the result in a new variable and save the value of base for each iteration.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Beginner problem- Integer based exponentiation

    public static int IntegerPower(int base, int power, int result)
    all you need in Exponential operations are base and exponent, why do you also have the result in your parameter?


    My code when run multiplies and then saves the number. (I.E. 2^2=4, but 2^3= 2*2=4 and 4 would get saved so the last calculation would be 4*4=16).
    your solution is wrong
    because you mean that 2^3 = 16?
    actually 2^3 is 8 not 16
    how?
    base = 2
    exponent = 3
    multiply the base by itself 3 times:
    base * base * base = answer
    2 * 2 * 2 = 8

    another example 2^4 = 16 how:
    base = 2
    exponent = 4
    multiply the base by itself 4 times
    base * base * base * base = answer
    2 * 2 * 2 * 2 = 16

    you can use scientific calculator for verification.


    The following is the code I believe the problem is in the while loop, as I don't know how to make it so it doesn't save the problem of the first calculation:
    the problem is your solution as what I told above:
            while(count<power)
            {
               base*=base;
               count++;
            }

Similar Threads

  1. Problem with String (and integer) conversion!
    By tonynsx in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 7th, 2013, 01:44 PM
  2. Integer.parseInt problem
    By 3therk1ll in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 28th, 2013, 06:28 AM
  3. Help with Fast Matrix Exponentiation
    By asundar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 18th, 2012, 03:41 PM
  4. beginner w/ text based RPG
    By rbread80 in forum Java Theory & Questions
    Replies: 12
    Last Post: December 4th, 2010, 02:39 PM
  5. Hex to Integer Problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 07:01 AM