Guest User

Untitled

a guest
Jan 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. //Lorenzo Gasparini 12/10/2012
  2. //Program that calculates the power of a number with exponent (and number) taken from input
  3. import java.util.Scanner; //importing library for keyboard input
  4.  
  5. class Es1_3
  6. {
  7.   public static double pow(double n, double m)
  8.   {
  9.     if (m==0) return 1;
  10.     return m>=0 ? n*pow(n,m-1) : pow(n,m+1)/n;
  11.   }
  12.  
  13.   public static void main (String[] argv)
  14.   {
  15.     Scanner input = new Scanner(System.in); //declarating the input handler
  16.    
  17.     System.out.println("This program calculates n^m.");
  18.     System.out.print("Insert n: ");
  19.     double n = input.nextDouble(); //taking the number from input
  20.    
  21.     System.out.print("Insert m: ");
  22.     double m = input.nextDouble(); //taking the exponent from input
  23.    
  24.     System.out.println("\nThe result is: "+pow(n,m));
  25.   }
  26. }
Add Comment
Please, Sign In to add comment