Search Logic Blocks

Saturday, January 4, 2020

Java: How to perform arithmetic operations on two integers - addition, subtraction, multiplication, division and remainder (modulo)

Problem : Write a program where user enters two integers and you can perform all operations - addition, subtraction, multiplication, division and remainder. And then prints all the results.

Solution : 

Class Arithmetic (Arithmetic.java)
import java.util.Scanner;

public class Arithmetic {
public static void main(String args[]) {
//Create Scanner object for input
Scanner sc = new Scanner(System.in);

// Asks to user to enter two integer numbers
System.out.print("Enter two integer numbers: ");

// Prompts user to enter first integer number
int operand1 = sc.nextInt();

// Prompts user to enter second integer number
int operand2 = sc.nextInt();

// Calculates sum by adding first number and second number
int sum = operand1 + operand2;

// Calculates difference by subtracting second number
// from first number
int difference = operand1 - operand2;

// Calculates product by multiplying first number and second number
int product = operand1 * operand2;

// Calculates quotient by dividing first number by second number
int quotient = operand1 / operand2;

// Calculates remainder by dividing first number
// by second number using remainder operator
int remainder = operand1 % operand2;

// Prints the sum
System.out.println("Sum: " + sum);

// Prints the difference
System.out.println("Difference: " + difference);

// Prints the product
System.out.println("Product: " + product);

// Prints the quotient
System.out.println("Quotient: " + quotient);

// Prints the remainder
System.out.println("Remainder: " + remainder);
}
}
Here is the output: -

Enter two integer numbers: 216 25
Sum: 241
Difference: 191
Product: 5400
Quotient: 8
Remainder: 16

Code can be found at Github Link.

No comments: