Write a Java program to calculate basic arithmetic operations

  • Estimate Completion Time
    not found 30 minutes
  • Difficulty Level:

Solution

import java.util.Scanner;
public class ClassName {
public static void main(String[] args) 
{
double num1;
		double num2;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter first number -");
		num1 = sc.nextDouble();
		System.out.println("Enter second number -");
		num2 = sc.nextDouble();

		double add= num1 + num2;
		double sub= num1 - num2;
		double multiply= num1 * num2;
		double divide= num1 / num2;
		
	System.out.println("\nAddition of two numbers is " + add);
	System.out.println("Subtraction of two numbers is " + sub);
	System.out.println("Multiplication of two numbers is " + multiply);
	System.out.println("Division of two numbers is " + divide);

}
}
Output
Enter first number -32
Enter second number -8

Addition of two numbers is 40.0
Subtraction of two numbers is 24.0
Multiplication of two numbers is 256.0
Division of two numbers is 4.0

Explanation

  • Two variables num1 and num2 are declared of data type “double”.
  • Scanner class is used to read the input from the user.
  • Calculate the addition, subtraction, multiplication, and division of the numbers entered by the user and stored in variables “add”, “sub”, “multiply” and “divide”.
  • And print the result using System.out.println() statement
QA Ads Banner

Proleed Academy

Proleed serves / offers professionally designed IT training courses
globally.

Copyright © 2023 - Proleed Academy | All Rights Reserved.