WAP in Java to accept 2 numbers from user and do addition and subtraction of numbers, display result on the screen.
Answers
Answered by
0
Answer:
import java.util.Scanner;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args) {
Scanner read=new Scanner(System.in);
try {
System.out.print("Enter the first number: ");
Integer num1=read.nextInt();
System.out.print("Enter the second number: ");
Integer num2=read.nextInt();
Integer sum=num1+num2;
Integer diff=num1-num2;
String str1=String.format("The sum is %d",sum);
String str2=String.format("The difference is %d",diff);
System.out.println(str1);
System.out.println(str2);
} catch (InputMismatchException e) {
System.out.println("Please enter a number and try again");
}
}
}
Similar questions
Math,
27 days ago
Math,
27 days ago
Hindi,
27 days ago
Social Sciences,
1 month ago
Computer Science,
1 month ago
Math,
9 months ago
Computer Science,
9 months ago
Math,
9 months ago