Calculator

Java program to Calculator using switch-case, methods, if statement, equals, and Menu Driven.

There are different ways to do simple calculator in Java.

  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

The program will prompt user to input the two numbers and then choose an operation (+, -, *, /) and then it will calculate the result using methods.

import java.util.Scanner;
public class CalculatorUsingMethod 
{
public int n1,n2;
public int ans = 0;
CalculatorUsingMethod()
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter First Number: ");
n1=scan.nextInt();
System.out.println("Enter Second Number: ");
n2=scan.nextInt();
}
void add()
{
ans=n1+n2;
System.out.println(n1+" "+ "+" +" "+n2+" = "+ans);
}
void sub()
{
ans=n1-n2;
System.out.println(n1+" "+ "-" +" "+n2+" = "+ans);
}
void mul()
{
ans=n1*n2;
System.out.println(n1+" "+ "*" +" "+n2+" = "+ans);
}
void div()
{
ans=n1/n2;
System.out.println(n1+" "+ "/" +" "+n2+" = "+ans);
}
public static void main(String[] args) 
{
CalculatorUsingMethod obj = new CalculatorUsingMethod();
Scanner scan = new Scanner(System.in);
System.out.println("Enter The Operator (+, -, *, /):  ");
char operator=scan.next().charAt(0);
switch(operator)
{
case '+' :
obj.add();
break;
case '-' :
obj.sub();
break;
case '*' :
obj.mul();
break;
case '/' :
obj.div();
break;
default:
System.out.println("You have entered wrong operator...");
}
}
}
Output:
Enter First Number: 
30
Enter Second Number: 
5
Enter The Operator (+, -, *, /):  
/
30 / 5 = 6

The program will prompt user to input the two numbers and then choose an operation(+, -, *, /) and then it will calculate the result using if statement.

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


The program will prompt user to input the two numbers and then choose an operation(+, -, *, /) and then it will calculate the result using menu driven. 

import java.util.Scanner;
public class CalciMenuDriven 
{
public static void main(String[] args) 
{
int n1,n2;
int ch=0,ans = 0;
int choice;
Scanner scan = new Scanner(System.in);
System.out.println("Enter First Number: ");
n1=scan.nextInt();
System.out.println("Enter Second Number: ");
n2=scan.nextInt();
do
{
System.out.println("Enter Your Choice\n  1. Addition\n  2. Substraction\n  
                                                            3. Multiplication\n  4. Division\n");
choice=scan.nextInt();
switch(choice)
{
case 1 :
ans=n1+n2;
System.out.println(n1+" "+ "+" +" "+n2+" = "+ans);
break;
case 2 :
ans=n1-n2;
System.out.println(n1+" "+ "-" +" "+n2+" = "+ans);
break;
case 3 :
ans=n1*n2;
System.out.println(n1+" "+ "*" +" "+n2+" = "+ans);
break;
case 4 :
ans=n1/n2;
System.out.println(n1+" "+ "/" +" "+n2+" = "+ans);
break;
default:
System.out.println("Wrong input....");
System.out.println("Exit...");
return;
}
}while(choice!=5);
}
}
Output:

Enter First Number: 
50
Enter Second Number: 
10
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

1
50 + 10 = 60
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

2
50 - 10 = 40
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

3
50 * 10 = 500
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

4
50 / 10 = 5
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

5
Wrong input....
Exit... 


The program will prompt user to input the two numbers and then choose an operation(+, -, *, /) and then it will calculate the result using equals.

    
import java.util.Scanner;
public class CalciUsingEquals 
{
	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 (+, -, *, /)...");
		String operation=s.next();
		System.out.println(n1+" "+ "*" +" "+n2+" = ");
			if(operation.equals("+"))
			{
				System.out.println(ans=n1+n2);
			}
			if(operation.equals("-"))
			{
				System.out.println(ans=n1-n2);
			}
			if(operation.equals("*"))
			{
				System.out.println(ans=n1*n2);
			}
			if(operation.equals("/"))
			{
				System.out.println(ans=n1/n2);
			}
	}
}

    
    
    Output:
    
    
    
    
Enter First Number...
10
Enter Second Number...
5
Enter Operator (+, -, *, /)...
/
10 * 5 = 2

    


The program will prompt user to input the two numbers and then choose an operation(+, -, *, /) and then it will calculate the result using switch...cases.

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

Pattern 31 & 32