This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Previously we have discussed the Program for
Java Program to find First Non Repeated Character In A String ?
Here we learn The Program for Calculator ,in this program we have use Scanner class to take input from user and we have use switch statement to know which case i.e Add /Sub /Div /Mul is to call for result.
import java.util.Scanner;
public class CalculatorProgram {
public static void main(String[] args) {
try {
System.out.println("--CALCULATOR--");
System.out.print("Enter First Number : ");
Scanner sc = new Scanner(System.in);
int firstNum = sc.nextInt();
System.out.print("Enter Second Number : ");
int secondNum = sc.nextInt();
System.out.println("\nSelect Number To Performs On Given Numbers--");
System.out.println("1 For Add(+)\n2 For Sub(-)\n3 For Mul(*)\n4 For Div(/)");
int operation = sc.nextInt();
switch (operation) {
case 1:{
System.out.println("Add(+) Result : "+(firstNum + secondNum));
break;
}
case 2:{
System.out.println("Sub(-) Result : "+(firstNum - secondNum));
break;
}
case 3:{
System.out.println("Mul(*) Result : "+(firstNum * secondNum));
break;
}
case 4:{
System.out.println("Div(/) Result : "+(firstNum / secondNum));
break;
}
default:
System.err.println("Please Select From 1 to 4 Only");
break;
}
} catch (Exception e) {
System.err.println("Please Enter Only Numbers");
}
}
}
Program Output :-
--CALCULATOR--
Enter First Number : 10
Enter Second Number : 15
Select Number To Performs On Given Numbers--
1 For Add(+)
2 For Sub(-)
3 For Mul(*)
4 For Div(/)
3
Mul(*) Result : 150
Java I/O Tutorial
No comments:
Post a Comment