Wednesday, February 1, 2023

Find the sum (addition), multiply, subtract, divide and remainder of two numbers

Write a Java program to print the sum (addition), multiply, subtract, divide and remainder of two numbers.

Test Data:
Input first number: 125
Input second number: 24

Given Solution:

The program follows he basic arithmetic operations formula. First both the numbers are taken as input from the user. Then all the formulas are applied in coding one by one and the result is printed on the screen.

Java Code:

import java.util.Scanner;

public class Exercise1 {

 public static void main(String[] args) {

  Scanner in = new Scanner(System.in);

  System.out.print("Input first number: ");

  int num1 = in.nextInt();

  System.out.print("Input second number: ");

  int num2 = in.nextInt();

  System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));

  System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));

  System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));

  System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));

  System.out.println(num1 + " mod " + num2 + " = " + (num1 % num2));

 }

}

Resulting Output:

Input first number: 125                                                                                        

Input second number: 24                                                                                       

125 + 24 = 149

125 - 24 = 101

125 x 24 = 3000

125 / 24 = 5

125 mod 24 = 5

No comments:

Post a Comment

Popular Posts