How to Create Basic Calculator using Java

0
60

This tutorial will teach you how to create a basic calculator using if statement in Java. This calculator can calculate using the four operations of mathematics.

I am using “if Statement” to identify what operation should use in calculating two numbers. And also a “Boolean” condition if statement, it means that only two results of the condition should display if it is the condition is true or false. I am also using a “Scanner” Function so that the user can input the values when the program is running. Please follow all the steps to complete this tutorial.

Basic Calculator Using If Statements Steps

  1. Create a new project.

2. Go to your source view and insert the code below above your class.

  1. import java.util.Scanner;

3. Inside your main method, declare five variables. Your code should look like below.

  1. Scanner input = new Scanner(System.in);
  2. int num1=0,num2=0,total=0;
  3. String op="";

Input – scanner variable

Num1 – handle the first value

Num2 – handle the second value

Total – display the result

Op – handle the input operation

4. Insert the statement below after step 3. This statement will ask the user input of first value, the second value, and operation.

  1. System.out.print("Enter first value: ");
  2. num1 = input.nextInt();
  3. System.out.print("Enter second value: ");
  4. num2 = input.nextInt();
  5. System.out.print("Enter operation: ");
  6. op = input.next();

5. Insert the statement below after step 4. The following statement will execute the algorithm and display the result.

  1. if (op.equals("a")){
  2. total = num1 + num2;
  3. System.out.println("The sum is " + total);
  4. }else if(op.equals("s")){
  5. total = num1 - num2;
  6. System.out.println("The difference is " + total);
  7. }else if(op.equals("m")){
  8. total = num1 * num2;
  9. System.out.println("The product is " + total);
  10. }else if(op.equals("d")){
  11. total = num1 / num2;
  12. System.out.println("The quotient is " + total);
  13. }else{
  14. System.out.println("Wrong operation!");
  15. }

6. Run your program and the result should look like the image below.

7. Complete Source Code.

  1. import java.util.Scanner;
  2. public class BasicCalculator {
  3. public static void main(String args[]){
  4. //declaring variables
  5. Scanner input = new Scanner(System.in);
  6. int num1=0,num2=0,total=0;
  7. String op="";
  8. //initializing the variables
  9. System.out.print("Enter first value: ");
  10. num1 = input.nextInt();
  11. System.out.print("Enter second value: ");
  12. num2 = input.nextInt();
  13. System.out.print("Enter operation: ");
  14. op = input.next();
  15. //runing the algo and display result
  16. if (op.equals("a")){
  17. total = num1 + num2;
  18. System.out.println("The sum is " + total);
  19. }else if(op.equals("s")){
  20. total = num1 - num2;
  21. System.out.println("The difference is " + total);
  22. }else if(op.equals("m")){
  23. total = num1 * num2;
  24. System.out.println("The product is " + total);
  25. }else if(op.equals("d")){
  26. total = num1 / num2;
  27. System.out.println("The quotient is " + total);
  28. }else{
  29. System.out.println("Wrong operation!");
  30. }
  31. }
  32. }

REMEMBER:
Operation “a” for addition, “s” for subtraction, “m” for multiplication, “d” for division. If you input operation in another format, the program will display message “Wrong operation”.

If you have any comments or suggestions about on How to Create Basic Calculator using Java, feel free to leave your comment below, use the contact page of this website or use my contact information.

Email: info@kmjtech.net  |  Cell. No.: 09468362375

Readers might check this:

Sum Two Integers Value using Java

Facebook Comments
(Visited 57 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here