Java Program for Calculator

In this tutorial we shall learn to create a Java powered calcultor. Just with the basic knowledge of control structures in Java, we can easily design a calculator in Java. All we need to know about using, If statement, While loop, input statement.

import java.util.*;

public class calc{
	public static void main(String args[]){
		Scanner in = new Scanner(;
			System.out.println("\nEnter your choice from above menu:");
			int choice = in.nextInt();
			if(choice == 6){
			System.out.println("\nEnter value 1:");
			float num1 = in.nextInt();
			System.out.println("\nEnter value 2:");
			float num2 = in. nextInt();
			float output;
				case 1: output = num1 + num2;
					System.out.println("\nAddition of above two numbers is:" + output);
				case 2: output = num1 - num2;
					System.out.println("\nSubtraction of above two numbers is:" + output);
				case 3: output = num1 * num2;
					System.out.println("\nMultiplication of above two numbers is:" + output);
				case 4: output = num1 / num2;
					System.out.println("\nDivision of above two numbers is:" + output);
				case 5: output = num1 % num2;
					System.out.println("\nRemainder for division of above two numbers is:" + output);

In the above program we ask the user, what type of operation they need to perform. After getting the choice of opertaion, we need to get the input of operand 1 and operand 2. Depeding on the choice of the user switch case is executed and required operation is done and obtained output is shown to the user.

Download the source code

This entry was posted in Java and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s