Thursday, 22 October 2020

Calculator using if

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



  1. Write a Java Program to make simple Calculator using methods
  2. Write a Java Program to make simple Calculator using if
  3. Write a Java Program to make a Menu Driven Calculator
  4. Write a Java Program to make simple Calculator using Equals
  5. Write a Java Program to make simple Calculator using switch...case

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32