Write a Java Program to make Calculator using if
import java.util.Scanner; public class CalciUsingIf { public static void main(String[] args) { int ans=0; Scanner s = new Scanner(System.in); System.out.println("Enter First Number..."); int n1=s.nextInt(); System.out.println("Enter Second Number..."); int n2=s.nextInt(); System.out.println("Enter Operator (+, -, *, /)..."); char op = s.next().charAt(0); System.out.println(n1+" "+ op +" "+n2+" = "); if(op=='+') { System.out.println(ans=n1+n2); } if(op=='-') { System.out.println(ans=n1-n2); } if(op=='*') { System.out.println(ans=n1*n2); } if(op=='/') { System.out.println(ans=n1/n2); } } }
Output:
Enter First Number...
5
Enter Second Number...
6
Enter Operator (+, -, *, /)...
+
5 * 6 = 11
No comments:
Post a Comment
If you have any doubts, please let me know