Producto Aprende programación con ejercicios Java

Lección:

Control del Flujo


Ejercicio:

Producto 70


Objetivo:

Cree un programa en java que pida al usuario dos números enteros y muestre su multiplicación, pero no usando "*". Debe utilizar sumas consecutivas. (Sugerencia: recuerde que 3 * 5 = 3 + 3 + 3 + 3 + 3 = 15)


Código:

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);
	}
}