Java (programming language)

Java program to print addition, subtraction, multiplication and division

Introduction

Java offers a versatile platform for performing basic arithmetic operations, including addition, subtraction, multiplication, and division. In this article, we’ll guide you through creating a Java program that takes two numbers as input, performs these four operations, and presents the results in a clear and organized manner.

Key Concepts Involved

  • Scanner Class: This class enables us to obtain user input from the console.
  • Arithmetic Operators: Java supports fundamental arithmetic operators: +, -, *, and /.
  • Printing Results: We’ll use System.out.println() to display the calculated results.

Step-by-Step Code Breakdown

  1. Import Scanner Class:

Java

import java.util.Scanner;
  1. Create Main Class and Method:

Java

public class ArithmeticOperations {
    public static void main(String[] args) {
  1. Instantiate Scanner Object:

Java

Scanner scanner = new Scanner(System.in);
  1. Declare Variables:

Java

int num1, num2;
  1. Prompt User for Input:

Java

System.out.print("Enter first number: ");
num1 = scanner.nextInt();
System.out.print("Enter second number: ");
num2 = scanner.nextInt();
  1. Perform Arithmetic Operations:

Java

int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
double quotient = (double) num1 / num2;  // Use double for accurate division
  1. Print Results:

Java

System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);

Full Code:

Java

import java.util.Scanner;

public class ArithmeticOperations {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int num1, num2;

        System.out.print("Enter first number: ");
        num1 = scanner.nextInt();
        System.out.print("Enter second number: ");
        num2 = scanner.nextInt();

        int sum = num1 + num2;
        int difference = num1 - num2;
        int product = num1 * num2;
        double quotient = (double) num1 / num2;

        System.out.println("Sum: " + sum);
        System.out.println("Difference: " + difference);
        System.out.println("Product: " + product);
        System.out.println("Quotient: " + quotient);
    }
}

Customization and Enhancements

  • Add error handling for invalid input (e.g., non-numeric characters).
  • Create a menu for users to select specific operations.
  • Implement a loop to repeat calculations for multiple sets of numbers.
  • Explore formatting options for output using System.out.printf().

Conclusion

Mastering basic arithmetic operations is fundamental in Java programming. This guide provides a clear and concise structure for performing these operations and displaying their results. By building upon this foundation and exploring the suggested enhancements, you can expand your Java programming skills and create more interactive and versatile applications.

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button