Advertisement
veronikaaa86

08. Math Power

Oct 5th, 2022
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. package methods;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.Scanner;
  5.  
  6. public class P08MathPower {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         double numInput = Double.parseDouble(scanner.nextLine());
  11.         double powerInput = Double.parseDouble(scanner.nextLine());
  12.  
  13.         double result = mathPower(numInput, powerInput);
  14.  
  15.         DecimalFormat df = new DecimalFormat("0.####");
  16.         System.out.println(df.format(result));
  17.     }
  18.  
  19.     public static double mathPower(double num, double power) {
  20.         // 2^4  -> 2 * 2 * 2 * 2
  21.         double result = 1;
  22.         for (int i = 1; i <= power; i++) {
  23.             result = result * num;
  24.         }
  25.  
  26.         return result;
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement