Thursday, May 13, 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.
Hereâ€™s the code:

```// Title: CALCULATOR
// 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 "System.in" as constructor argument
Scanner n = new Scanner(System.in);

while (true)
{
System.out.println("t2.subtraction");
System.out.println("t3.division");
System.out.println("t4.multiplication");
System.out.print("t5.Exit Program");
System.out.print("nt.Enter Choice : ");
menuChoice = n.nextInt(); // n.nextInt() function takes integer input

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 );
break;
} // end case 1

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

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

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

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

default:
{
System.out.print("Invalid Input, try again");
break;
}  // 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:

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....