java:User to enter two integers, obtains them from the user and prints their sum, product, difference and quotient (division).

(Arithmetic) Write an application that asks the user to enter two integers, obtains them from the user and prints their sum, product, difference and quotient (division).

Arithmetic java user to enter two integers, obtains them from the user and prints their sum

public class Arithmetic 
{
	// main method begins execution of Java application
	public static void main(String[] args) 
	{
		// create Scanner to obtain input from command window
		Scanner input=new Scanner(System.in);
		
		// Initialize variable
		int num1;	// first integer for user input
		int num2;	// second integer for user input
		int sum;	// sum of num1 and num2
		int product;	// product of num1 and num2
		int difference;	// difference of num1 and num2
		int quotient;	// quotient of num1 and num2
		
		System.out.print("Enter first integer: ");	// prompt
		num1 = input.nextInt();	// read first number from user
		
		System.out.print("Enter second integer: ");	// prompt
		num2 = input.nextInt();	// read second number from user
		
		sum	= num1 + num2;	// add numbers
		product	= num1 * num2;	// multiply numbers
		difference = num1 - num2;	// difference of numbers
		quotient = num1 / num2;	// division of numbers
		
		System.out.printf("Sum is %d\n", +sum);	// display sum
		System.out.printf("Product is %d\n", +product);	// display product
		System.out.printf("Difference is %d\n", +difference);	// display difference
		System.out.printf("Quotient is %d\n", +quotient);	// display quotient
		
	}	// end method main

}	// end class Arithmetic

/* Output:

  • Enter first integer: 5
  • Enter second integer: 10
  • Sum is 15
  • Product is 50
  • Difference is -5
  • Quotient is 0
    • Enter first integer: 6
  • Enter second integer: 4
  • Sum is 10
  • Product is 24
  • Difference is 2
  • Quotient is 1
    */

Arithmetic java

import java.util.Scanner;

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

        int difference = 0;
        int quotient = 0;

        System.out.print("Enter 2 integers separated by a space: ");
        int x = input.nextInt();
        int y = input.nextInt();

        // sum
        printSolution("Sum", x + y);

        // product
        printSolution("Product", x * y);

        // difference - take absolute value to avoid negative numbers
        printSolution("Difference", Math.abs(x - y));

        if(x != 0 && y != 0)
            printSolution("Quotient", y % x);
        else
            printSolution("Quotient error: Cannot divide by zero", 0);
    }
    // show the solution/any error messages (division by zero)
    private static void printSolution(String message, int value){
        System.out.printf("%s = %d\n", message, value);
    }
}