Web Analytics Made Easy -
StatCounter Exponents in Java without Math.pow - CodingForum


No announcement yet.

Exponents in Java without Math.pow

  • Filter
  • Time
  • Show
Clear All
new posts

  • Exponents in Java without Math.pow

    As I already said in the JavaScript forum, (by mistake), I am trying to learn Java on my own. I bought a book at Barnes and Noble, and now I am trying to do a base with exponent without using the Math.pow. I figured I needed to use a loop. I was correct according to the other people. I am simulating an IRA account starting at 16, figuring on 60 and 61, this is what I have so far, but I am lost... I am not understanding it isn't calculating the 44 yrs. I know I need more code to calculate the age of 61, but I figured if I could get the age 60 code to work, I could just copy it again.

    import java.io.*;
    import java.util.*;

    public class Ch5Prob18

    static Scanner console = new Scanner(System.in);

    static final double interest = .10;

    public static void main(String[] args)
    double initialDep;
    double whenS;
    double whenSo;
    double interestWithdrawn;
    double income;
    double time;
    int y;
    int period;

    System.out.println("Please enter the amount of the initial deposit when you were 16: ");
    initialDep = console.nextDouble();

    {time = 1;
    for (y = 1; y <= 45; ++y)
    time = time * interest;

    whenS = time * initialDep;

    //whenS = (initialDep * (1 + interest));<--I had this multiplied by 45, but it wasn't the correct formula***
    whenSo = (initialDep * (1 + interest));
    interestWithdrawn = (((whenSo * interest) + whenSo) - whenSo);

    System.out.printf("The initial investment was $%.2f", initialDep);
    System.out.printf(". The total amount accumulated" + " after 44 years(when your 60), \n if $%.2f", initialDep);
    System.out.printf(" is allowed to compound with an interest of 10.00%%, comes to $%.2f", whenS);
    System.out.print(". ");
    System.out.println("\n ");
    System.out.printf("The total amount accumulated after 45 years(when your 61), if $%.2f", initialDep);
    System.out.print(" is allowed to compound \n");
    System.out.printf(" with an interest of 10.00%%, comes to $%.2f", whenSo);
    System.out.printf(". The interest earned during this year is $%.2f", interestWithdrawn);
    System.out.print(". \n");
    System.out.printf("If interest is withdrawn each year thereafter, your income is $%.2f", interestWithdrawn);
    System.out.print(" per month.");


  • #2
    Still not sure why you can't use Math.pow()... of course, you could always do Math.exp(y*Math.log(x)) for x^y. :-D


    • #3
      It worked!! Thank you!!