need help
The â??powerâ?? function currently does not exist in IJVM. For example, if two numbers 2 and 3 are on the stack, the power function would compute 2 power 3, i.e. 23 that should give 8 as the result. Assume two positive numbers (say, a and b) are provided on the stack (with a being the first value and b being the value on top). Write an IJVM code segment that will calculate the power of the two numbers (ab) and put the result back on the stack. You are allowed to use existing IJVM instructions only. Include appropriate comments to explain your programm output
import java.util.*; public class Calculate { public static int power(int a, int b) { int answer = 1; for(int i = 0; i < b; i++) { answer *= a; } return answer; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Num1: "); int num1 = input.nextInt(); System.out.print("Num2: "); int num2 = input.nextInt(); if (num1 > 0 && num2 > 0) { int sum = num1 + num2; System.out.println("Sum= " + sum); int diff = 0; if (num1 > num2) { diff = num1 - num2; } else { diff = num2 - num1; } System.out.println("Difference= " + diff); int prod = num1 * num2; System.out.println("Product= " + prod); int pow = Calculate.power(num1,num2); System.out.println("power= " + pow); } else { System.out.println("Invalid Numbers"); System.out.println("End of program"); } } }
Ads