Product Learn programming Java



Lesson:

Flow Control


Exercise:

Product


Objetive:

Create a java program that asks the user for two integer numbers and shows their multiplication, but not using "*". It should use consecutive additions. (Hint: remember that 3 * 5 = 3 + 3 + 3 + 3 + 3 = 15)


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		System.out.print("Enter the first number: ");
		int n1 = Integer.parseInt(new Scanner(System.in).nextLine());

		System.out.print("Enter the second number: ");
		int n2 = Integer.parseInt(new Scanner(System.in).nextLine());

		int result = 0;
		int i = 0;

		while (i < n2)
		{
			result = result + n1;
			i++;
		}
		System.out.printf("%1$s X %2$s = %3$s" + "\r\n", n1, n2, result);
	}
}



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