Results 1 to 3 of 3
  1. #1
    Reviloest is offline Member
    Join Date
    Jan 2016
    Posts
    13
    Rep Power
    0

    Default [Java] How to do exponentiations?

    Hey there!

    I dont know how to do the exponentiations in java. I know how it works (math.pow(x,y)), but it aint so easy.
    Code:
    Java Code:
    import java.util.Scanner;
     
    public class Problem {
     
      public static void main(String[] args) {
     
        Scanner scan = new Scanner(System.in);
     
        System.out.println("Please enter the loan in euros:  ");
        double s = scan.nextDouble();
    	
    	System.out.println("Please enter the loan period in years:  ");
        double y = scan.nextDouble();
    	
    	System.out.println("Please enter the interest rate in %:  ");
        double p = scan.nextDouble();
    	
    	double k = ( s * ( p / 100 / 12 )) / ( 1 - ( 1 + ( p / 100 / 12 )) ^ ( -y * 12 )); //i dont know how do the exponentation.
    	System.out.printf("%.2f" , k);
    
    	double t = k * y * 12;
    	System.out.printf("%.2f", t);
    	
        System.out.printf("With a loan of %.2d euros %.2d interest rate you have to pay %.2d + euros per month %.2d + years. You pay the pank a total of %.2d euros); //i want 2 decimal spaces but it doesnt do it.
      }
     
    }
    The last printf() is also probobally wrong aswell, as i've used it before, but it doesnt change how the valuables are displayed. Help appreciated!
    Last edited by Reviloest; 01-09-2016 at 01:58 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: [Java] How to do exponentiations?

    Java doesn't have an exponentiation operator; there's a statIC pow(x, y) method in the Math class for that purpose. Read the API documentation for that class and method for details.

    kind regards,

    Jos

    ps. the arguments for that printf( ... ) method again don't make sense; read your other thread.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: [Java] How to do exponentiations?

    Can you post the program's output and add some comments showing what is wrong with it and show what you want the output to look like?
    If you don't understand my response, don't ignore it, ask a question.

Posting Permissions

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