🧮 Simple Calculator Program in Java
In this tutorial, we’ll build a simple calculator program in Java using basic arithmetic operations. This is a great exercise for beginners who want to practice reading user input and performing simple calculations.
We’ll be using IntelliJ IDEA Community Edition — it’s free to use and easy to get started with. You can search and download it via Google. 🔍
Let’s begin by importing the required library to take input from the console:
Java:
import java.util.Scanner;
Place this import at the top of your Java class file. Then, we’ll start writing the program inside the
main method.Here is the full code:
Java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number: ");
int firstNumber = s.nextInt();
System.out.print("Enter the second number: ");
int secondNumber = s.nextInt();
System.out.println("Sum: " + (firstNumber + secondNumber));
System.out.println("Difference: " + (firstNumber - secondNumber));
System.out.println("Product: " + (firstNumber * secondNumber));
System.out.println("Quotient: " + (firstNumber / secondNumber));
System.out.println("Calculation complete!");
}
}
✅ What This Program Does:
- Takes two integers from the user.
- Performs addition, subtraction, multiplication, and division.
- Displays the result of each operation.
This program is a great way to practice console input, working with variables, and using basic operators in Java.
Happy coding! ☕