RosettaCodeData/Task/Arithmetic-Integer/Java/arithmetic-integer.java

23 lines
1.1 KiB
Java
Raw Normal View History

2013-04-10 15:42:53 -07:00
import java.util.Scanner;
2017-09-23 10:01:46 +02:00
public class IntegerArithmetic {
public static void main(String[] args) {
2018-06-22 20:57:24 +00:00
// Get the 2 numbers from command line arguments
2017-09-23 10:01:46 +02:00
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
2018-06-22 20:57:24 +00:00
int sum = a + b; // The result of adding 'a' and 'b' (Note: integer addition is discouraged in print statements due to confusion with string concatenation)
int difference = a - b; // The result of subtracting 'b' from 'a'
int product = a * b; // The result of multiplying 'a' and 'b'
int division = a / b; // The result of dividing 'a' by 'b' (Note: 'division' does not contain the fractional result)
int remainder = a % b; // The remainder of dividing 'a' by 'b'
2017-09-23 10:01:46 +02:00
System.out.println("a + b = " + sum);
2018-06-22 20:57:24 +00:00
System.out.println("a - b = " + difference);
System.out.println("a * b = " + product);
System.out.println("quotient of a / b = " + division); // truncates towards 0
System.out.println("remainder of a / b = " + remainder); // same sign as first operand
2017-09-23 10:01:46 +02:00
}
2013-04-10 15:42:53 -07:00
}