Product Learn programming with Java exercises

Lesson:

Flow Control


Exercise:

Product 62


Objetive:

Create a java program which asks the user for two integer numbers and displays their multiplication, but not using "*". You must use consecutive sums. (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);
	}
}