Posted by : at

Category : programming   java

Today we will create a program to do basic calculations like addition, substraction, multiplication and division in Java.

package tuts;

import java.util.Scanner;

public class calculator {

public static void main(String[] args) {

// define integers

double num1 , num2;

int option;

// Create Scanner Object

Scanner scanner = new Scanner(System.in);

System.out.print("Menu: \n 1. Add \n 2. Substract \n 3. Multiply \n 4. Divide");
System.out.print("\n Enter Your Choice");

// Take a single digit input
option = scanner.nextInt();

System.out.print("Enter First Number ");

num1 = scanner.nextDouble();

System.out.print("Enter Second Number ");

num2 = scanner.nextDouble();

scanner.close();

double result;

switch(option) {

case 1:
result= num1 + num2;
System.out.println("The Addition of two numbers is " + result);
break;

case 2:
result = num1 - num2;
System.out.println("The Subtraction of two numbers is " + result);
break;

case 3:
result = num1 * num2;
System.out.println("The Multiplication of two numbers is " + result);
break;

default:
System.out.println("You have choosen a Invalid Option");
break;
   }
 } 
}