Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How can I calculate the balance with this formula in java program

    If you deposit p dollars, the money in your account after n years at an interest rate r is p (1 + r)^n


    This is what I got.... Is this correct? and would I need to cast this beacause years are in int variable?

    double balance;
    double dollars;
    double interestRate;
    int years;

    balance = dollars * Math.pow((1 + interestRate), years);

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,930
    Thanks
    46
    Thanked 203 Times in 202 Posts
    years does need to be in double form- whether you convert or cast in this situation i think depends on your style since double is more precise than int and there should be no chance of data loss (I could be wrong though)

    Edit: Math.pow(double a, double b) is expected

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

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