Java program that computes the sum, difference, product, average, distance, maximum and minimum of two numbers
Posted by Samath
Last Updated: February 08, 2021

Write a program that prompts the user for two integers and then prints
 • The sum
 • The difference
 • The product
 • The average
 • The distance (absolute value of the difference)
 • The maximum (the larger of the two)
 • The minimum (the smaller of the two)
 Hint: The max and min functions are declared in the Math class.

Example:

Please enter the 1st number: 5                    
Please enter the 2nd number: 10                                                                                                                                                 
Sum: 15.00               
Difference: -5.00                   
Product: 50.00         
Average: 7.50          
Distance: 5.00         
Maximum: 10.00        
Minimum: 5.00

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
	    
		Scanner input = new Scanner(System.in);
		
		double num1;
		double num2;
		double sum;
		double difference;
		double product;
		double average;
		double distance;
		double maximum;
		double minimum;
		
		System.out.print("Please enter the 1st number: ");
		num1 = input.nextDouble();
		
		System.out.print("Please enter the 2nd number: ");
		num2 = input.nextDouble();
		
		input.close();
		
		sum = num1 + num2;
		difference = num1 - num2;
		product = num1 * num2;
		average = (num1 + num2) / 2;
		distance = Math.abs(difference);
		maximum = Math.max(num1, num2);
		minimum = Math.min(num1, num2);
		
		System.out.printf("\nSum: %.2f\n", sum);
				
		System.out.printf("Difference: %.2f\n",difference);
		
		System.out.printf("Product: %.2f\n", product);
		
		System.out.printf("Average: %.2f\n", average);
		
		System.out.printf("Distance: %.2f\n", distance);
		
		System.out.printf("Maximum: %.2f\n", maximum);
		
		System.out.printf("Minimum: %.2f", minimum);
	}
}
Related Content