Equivalent operations Learn programming Java

Lesson:

First contact with Java


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.


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int num1, num2, num3;
		System.out.print("Enter first number....");
		num1 = Integer.parseInt(new Scanner(System.in).nextLine());
		System.out.print("Enter second number....");
		num2 = Integer.parseInt(new Scanner(System.in).nextLine());
		System.out.print("Enter third number....");
		num3 = Integer.parseInt(new Scanner(System.in).nextLine());
		System.out.printf("Result of operation between %1$s," + 
        " %2$s and %3$s, (a+b)·c is %4$s and a·b + a·c is %5$s", 
        num1, num2, num3, ((num1 + num2) * num3), (num1 * num3 + num2 * num3));
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.