User should be prompted to enter two non-negative numbers (say, a and b where a >0 and b > 0) line by line and then press the enter key. Your program should calculate and display the following for these numbers (see sample output below): Sum (a + b) Difference (a - b or b - a, whichever gives a positive value) Product (a x b) Power (ab) OUTPUT Num1: 2 Num2: 10 Sum = 12 Difference = 8 Product = 20 Power = 1024 Num1: a Invalid Number Num1: x End of program
import java.util.*;
public class Calculate {
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);
double pow = Math.pow(num1, num2);
System.out.println("power= " + pow);
} else {
System.out.println("Invalid Number");
System.out.println("End of program");
}
}
}