Skip to content

MyUstaadG

Learn to Serve

  • Computer Science
    • Software
    • Computer Tips
    • Coding
      • Conditional Structures Exercises
      • Loops Exercises
      • Arrays Exercises
      • Python Exercises
    • Microsoft Office
      • MS Excel
      • MS Word
  • Videos
    • Funny Videos
    • Health and Fitness Videos
    • PUBG Videos
    • Technology Videos
  • News
  • Funny
  • Memes
  • Playwright Tutorials
  • Tutorials
  • Toggle search form
Write a Program to Build a Simple Calculator in Java

Write a Program to Make a Simple Calculator in Java Programming Language | Solved Programming Exercises

Posted on September 26, 2021September 26, 2021 By myustaadg.com
Spread the love

Write a Program to Make a Simple Calculator in Java Programming Language | Solved Programming Exercises

In this tutorial we will write a program in Java Language to build a simple calculator. We will take two numbers from user and an operator. if operator is ‘+’ then add both numbers and so on.

We will use Netbeans IDE For Development. It is very simple and easy to use IDE for Java Development.

How to install Netbeans IDE? How to Install JDK? Learn From following video.

How to install Netbeans and JDK

So let’s Code in Java Programming Language.

Open Netbeans IDE and Create a Java New Project “CalculatorinJava”. It will show following code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package calculatorinjava;

import java.util.Scanner;

/**
 *myustaadg.com
 * @author mohammadumar
 */
public class CalculatorinJava {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
   {
      
    }
    
}

Now lets take input from user. To take input in Java From user we will use Scanner Class.

Scanner scan = new Scanner(System.in);
       int a,b;
       char op;
       System.out.print("Enter First Number = ");
       a = scan.nextInt();
       System.out.print("Enter Any Operator(+,-,*,/) = ");
       op = scan.next().charAt(0);
       System.out.print("Enter Second Number = ");
       b = scan.nextInt();

In above code, we created Scanner class object to take input from user.

Now let’s apply if-else if condition to check whether an operator is +, -, *, /.

if(op == '+')
       {
         System.out.println("Sum = " + (a+b));  
       }
       else if(op == '-')
       {
           System.out.println("Minus = " + (a-b));
       }
       else if(op == '*')
       {
           System.out.println("Mul = " + (a*b));
       }
       else if(op == '/')
       {
           if(b == 0)
           {
               System.out.println("Cannot Divide by Zero");
           }
           else
           {
               System.out.println("Sum = " + (a/b));
           }
       }
       else
       {
           System.out.println("Invalid Operator");
       }
       

Write a Program to Make a Simple Calculator in C++ Programming Language | Solved Programming Exercises

Write a Program to Make a Simple Calculator in C# Programming Language | Solved Programming Exercises

Copy the Complete Java Code From Here.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package calculatorinjava;

import java.util.Scanner;

/**
 *  myustaadg.com
 * @author mohammadumar
 */
public class CalculatorinJava {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       int a,b;
       char op;
       System.out.print("Enter First Number = ");
       a = scan.nextInt();
       System.out.print("Enter Any Operator(+,-,*,/) = ");
       op = scan.next().charAt(0);
       System.out.print("Enter Second Number = ");
       b = scan.nextInt();
       if(op == '+')
       {
         System.out.println("Sum = " + (a+b));  
       }
       else if(op == '-')
       {
           System.out.println("Minus = " + (a-b));
       }
       else if(op == '*')
       {
           System.out.println("Mul = " + (a*b));
       }
       else if(op == '/')
       {
           if(b == 0)
           {
               System.out.println("Cannot Divide by Zero");
           }
           else
           {
               System.out.println("Sum = " + (a/b));
           }
       }
       else
       {
           System.out.println("Invalid Operator");
       }
       
    }
    
}

Hope you will learn something from Here.

Write a Program to Make a Simple Calculator in Java Programming Language. Watch the following Video.

Write a Program to Make a Simple Calculator in Java Programming Language

Programming Exercises Tags:calculator in java, java programming, Java programming exercises, java programming lectures, mohammad umar, myustaadg, program to build a calculator, programming in java, Simple Calculator in Java, Solved Programming Exercises, solved programming exercises in java, Write a Program to Build a Simple Calculator in Java, Write a Program to Make a Simple Calculator in Java Programming Language

Post navigation

Previous Post: Write a Program to Make a Simple Calculator in C++ Programming Language | Solved Programming Exercises
Next Post: Write a Program to Make a Simple Calculator in C# Programming Language | Solved Programming Exercises

More Related Articles

Write a program that inputs real values into float type array. It reads the value of each element of float type array and assigns to int type array. Arrays Exercises
Write a program that inputs values into a one-dimensional array and finds odd and even Numbers. Arrays Exercises
Write a program that initializes values 2, 6, 3, 5, 4 and 7 in one-dimensional array and computes the factorial. Arrays Exercises
Write a Program in C++ to Calculate Your Age in Years Months and Days using Date of Birth Programming Exercises
Write a program that inputs values into an array and finds the smallest and largest value from it. Arrays Exercises
Write a Program to Build a Simple Calculator in C++ Write a Program to Make a Simple Calculator in C++ Programming Language | Solved Programming Exercises Programming Exercises

Copyright © 2022 MyUstaadG.

Powered by PressBook Blog WordPress theme