Product Learn programming C#

Lesson:

Flow Control


Exercise:

Product


Objetive:

Create a C# 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:

using System;
public class exercis40
{
    public static void Main()
    {
        Console.Write("Enter the first number: ");
        int n1 = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter the second number: ");
        int n2 = Convert.ToInt32(Console.ReadLine());

        int result = 0;
        int i = 0;

        while (i < n2)
        {
            result = result + n1;
            i++;
        }
        Console.WriteLine("{0} X {1} = {2}", n1, n2, result);
    }
}

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