Thursday, March 4, 2021
Home College Assignments Creating a Calculator program in Java

Creating a Calculator program in Java

Java, being a power full language, contributes in many technology fields. It has deep contribution in the modern world. A basic calculator program in java can perform logical computations and displays the output. Getting curious about how it works?
You can Download the Source Code Below of see the code live.
downloadHere’s the code:

// Environment : Java
// Programming Tunes
import java.util.Scanner; // Scanner class used to take input
class Calculator
	public static void main (String[] args)
		//'n' as a Scanner class object with "" as constructor argument
		Scanner n = new Scanner(;
		int operand_1,operand_2,menuChoice, result;

		while (true)
			System.out.print("t5.Exit Program");
			System.out.print("nt.Enter Choice : ");
			menuChoice = n.nextInt(); // n.nextInt() function takes integer input

			if (menuChoice==5)
				System.exit(1); // terminate program

			System.out.print("Enter First Operand :");
			operand_1 = n.nextInt(); 

			System.out.print("Enter First Operand :");
			operand_2 = n.nextInt();

			case 1:
				result = operand_1 + operand_2 ;
				System.out.print("Sum :" + result );
			} // end case 1

			case 2:
				result = operand_1 - operand_2 ;
				System.out.print("Difference :" + result );
			} // end case 2

			case 3:
				result = operand_1 / operand_2 ;
				System.out.print("Division :" + result );
			} // end case 3

			case 4:
				result = operand_1 * operand_2 ;
				System.out.print("Product :" + result );
			}  // end case 4

			case 5:
				System.out.print("Safe Exit" );
			}  // end case 5

				System.out.print("Invalid Input, try again");
			}  // end default

			} // end switch

		} // end while loop

	} // end main

} // end Calculator class

The above code provides basic calculations like addition, subtraction, multiplication and division. The main function act as a menu for the calculator program. First step is to select a menu option, then it takes two operands and calculates its result. The program will run again and again until the exit is option is selected. Calculator Program in Java output is shown below:
calculator in java output

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....