Java Tutorial
In java programming, Math.pow method is used to calculate power.
This java program is used to calculate the power for the user inputs.
import java.util.Scanner; public class Power { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Power Computation"); System.out.println("------------------"); System.out.print("Number: "); double number = in.nextDouble(); System.out.println("Power: "); double pow = in.nextDouble(); double result = Math.pow(number, pow); System.out.println("Power ("+number+", "+pow+"): "+result); } }Output:
D:\Java_Programs>javac Power.java D:\Java_Programs>java Power Power Computation ------------------ Number: 4 Power: 3 Power (4.0, 3.0): 64.0
import java.util.Scanner; public class Power { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Power Computation"); System.out.println("------------------"); System.out.print("Number: "); int number = in.nextInt(); System.out.println("Power: "); int pow = in.nextInt(); int result = (int)Math.pow((double)number, (double)pow); System.out.println("Power ("+number+", "+pow+"): "+result); } }Output:
D:\Java_Programs>javac Power.java D:\Java_Programs>java Power Power Computation ------------------ Number: 2 Power: 4 Power (2, 4): 16
import java.util.Scanner; public class Power { public static int power(int number, int pow){ int powerVal = 1; for(int i=1;i<=pow;i++){ powerVal *= number; } return powerVal; } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Power Computation"); System.out.println("------------------"); System.out.print("Number: "); int number = in.nextInt(); System.out.println("Power: "); int pow = in.nextInt(); int result = power(number, pow); System.out.println("Power ("+number+", "+pow+"): "+result); } }Output:
D:\Java_Programs>javac Power.java D:\Java_Programs>java Power Power Computation ------------------ Number: 3 Power: 4 Power (3, 4): 81
import java.util.Scanner; public class Power { public static int power(int number, int pow){ int powerVal = 1; int i=1; while(i<=pow){ powerVal *= number; i++; } return powerVal; } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Power Computation"); System.out.println("------------------"); System.out.print("Number: "); int number = in.nextInt(); System.out.println("Power: "); int pow = in.nextInt(); int result = power(number, pow); System.out.println("Power ("+number+", "+pow+"): "+result); } }Output:
D:\Java_Programs>javac Power.java D:\Java_Programs>java Power Power Computation ------------------ Number: 5 Power: 3 Power (5, 3): 125
import java.util.Scanner; public class Power { public static int power(int number, int pow){ int powerVal = 1; int i=1; do { powerVal *= number; i++; } while(i<=pow); return powerVal; } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Power Computation"); System.out.println("------------------"); System.out.print("Number: "); int number = in.nextInt(); System.out.println("Power: "); int pow = in.nextInt(); int result = power(number, pow); System.out.println("Power ("+number+", "+pow+"): "+result); } }
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page