Exercise
Equivalent operations
Objetive
Write a java program to ask the user for three numbers (a, b, c) and display the result of (a+b)·c and the result of a·c + b·c.
Example Code
// Importing the Scanner class to handle input from the useimport java.util.Scanner;
public class Program {
// Entry point of the program
public static void main(String[] args) {
// Create a scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the first number (a)
System.out.print("Enter the first number (a): ");
// Read the user's input as a double
double a = scanner.nextDouble();
// Prompt the user to enter the second number (b)
System.out.print("Enter the second number (b): ");
// Read the user's input as a double
double b = scanner.nextDouble();
// Prompt the user to enter the third number (c)
System.out.print("Enter the third number (c): ");
// Read the user's input as a double
double c = scanner.nextDouble();
// Calculate (a + b) * c
double result1 = (a + b) * c;
// Calculate a * c + b * c
double result2 = a * c + b * c;
// Display the results
System.out.println("The result of (a + b) * c is: " + result1);
System.out.println("The result of a * c + b * c is: " + result2);
// Close the scanner to free resources
scanner.close();
}
}