Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lorenzo Gasparini 12/10/2012
- //Program that calculates the power of a number with exponent (and number) taken from input
- import java.util.Scanner; //importing library for keyboard input
- class Es1_3
- {
- public static double pow(double n, double m)
- {
- if (m==0) return 1;
- return m>=0 ? n*pow(n,m-1) : pow(n,m+1)/n;
- }
- public static void main (String[] argv)
- {
- Scanner input = new Scanner(System.in); //declarating the input handler
- System.out.println("This program calculates n^m.");
- System.out.print("Insert n: ");
- double n = input.nextDouble(); //taking the number from input
- System.out.print("Insert m: ");
- double m = input.nextDouble(); //taking the exponent from input
- System.out.println("\nThe result is: "+pow(n,m));
- }
- }
Add Comment
Please, Sign In to add comment