Write a Java Program to make Calculator using switch...case
import java.util.Scanner; public class calculator { public static void main(String[] args) { int n1,n2; int ans = 0; Scanner scan = new Scanner(System.in); System.out.println("Enter First Number: "); n1=scan.nextInt(); System.out.println("Enter Second Number: "); n2=scan.nextInt(); System.out.println("Enter The Operator (+, -, *, /): "); char operator=scan.next().charAt(0); switch(operator) { case '+' : ans=n1+n2; break; case '-' : ans=n1-n2; break; case '*' : ans=n1*n2; break; case '/' : ans=n1/n2; break; default: System.out.println("Wrong input...."); System.out.println("Exit..."); } System.out.println(n1+" "+ "*" +" "+n2+" = "+ans); } }
Output:
Enter First Number:
10
Enter Second Number:
20
Enter The Operator (+, -, *, /):
*
10 * 20 = 200
No comments:
Post a Comment
If you have any doubts, please let me know