Menu Close

Assignment Operator Example in Java

Sneha got a single integer as an input , and her task to perform all assignment operations in with the given input . Let us help her to solve with the following conditions met, To Perform ,

Assign an input the new the variable and display
Using addition assignment operation
Using Subtraction assignment operation
Using division assignment operation
Using modulus assignment operation
Input and Output Format:

Refer sample input and output for formatting specification.All float values are displayed correct to 2 decimal places.All text in bold corresponds to input and the rest corresponds to output.

import java.io.*;
import java.util.*;
public class temp {
	 public static void main(String[] args) {
     Scanner s=new Scanner(System.in);
     System.out.print("Enter Value 1: ");
     int a=s.nextInt();
     System.out.print("Enter Value 2: ");
     int b=s.nextInt();
     int add=a+b, sub=a-b, mul=a*b;
     double div=a/b, mod=a%b;
     System.out.println("Addition: "+add+"\nSubtraction: "+sub+"\nMultiplication: "+mul);
     System.out.println("Division: "+String.format("%.2f",div)+"\nModulus: "+String.format("%.2f",mod));
	}
}

INPUT_1:
Enter Value 1:  6
Enter Value 2:  5

OUTPUT:
Addition:  11
Subtraction:  1
Multiplication:  30
Division:  1.00
Modulus:  1.00


INPUT_2:
Enter Value 1:  66
Enter Value 2:  11

OUTPUT:
Addition:  77
Subtraction:  55
Multiplication:  726
Division:  6.00
Modulus:  0.00


INPUT_3:
Enter Value 1:  2
Enter Value 2:  2

OUTPUT:
Addition:  4
Subtraction:  0
Multiplication:  4
Division:  1.00
Modulus:  0.00


INPUT_4:
Enter Value 1:  8965
Enter Value 2:  25

OUTPUT:
Addition:  8990
Subtraction:  8940
Multiplication:  224125
Division:  358.00
Modulus:  15.00


INPUT_5:
Enter Value 1:  9865
Enter Value 2:  36

OUTPUT:
Addition:  9901
Subtraction:  9829
Multiplication:  355140
Division:  274.00
Modulus:  1.00


INPUT_6:
Enter Value 1:  896
Enter Value 2:  9

OUTPUT:
Addition:  905
Subtraction:  887
Multiplication:  8064
Division:  99.00
Modulus:  5.00


ILLUSTRATION

Executed using javac linux terminal