Ejercicio
Producto
Objetivo
Cree un programa en C# 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 de Ejemplo
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
// Prompt the user to enter the first integer number
Console.Write("Enter the first number: ");
int num1 = int.Parse(Console.ReadLine()); // Read and convert the input to an integer
// Prompt the user to enter the second integer number
Console.Write("Enter the second number: ");
int num2 = int.Parse(Console.ReadLine()); // Read and convert the input to an integer
int result = 0; // Initialize a variable to store the result of the multiplication
// Loop to add the first number num2 times
for (int i = 1; i <= Math.Abs(num2); i++) // Loop from 1 to the absolute value of num2
{
result += num1; // Add num1 to result in each iteration
}
// If num2 is negative, the result should be negative as well
if (num2 < 0)
{
result = -result; // Negate the result if num2 is negative
}
// Display the result of the multiplication
Console.WriteLine($"The product of {num1} and {num2} is: {result}");
}
}