Product - C# Programming Exercise

This C# exercise aims to develop a program that asks the user for two integer numbers and shows their multiplication, but without using the "*" operator. Instead, the program should perform the multiplication using consecutive additions. This type of exercise is fundamental for understanding the basic concept of multiplication, as it simulates it through a repetitive process of addition. The exercise strengthens the importance of control logic and loops in C#, as it is necessary to use a loop to repeatedly add one number as many times as the value of the second number. This exercise is useful for enhancing the understanding of how to perform mathematical operations without relying on standard operators and how to program solutions that solve problems using simple algorithms. Additionally, it is a good starting point to improve programming skills and optimize processes that involve repetitive calculations.

 Category

Flow Control

 Exercise

Product

 Objective

Write 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)

 Write Your C# Exercise

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

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Absolute value

    This C# exercise aims to develop a program that calculates (and displays) the absolute value of a number x. The absolute value of a number is its distan...

  •  Hollow rectangle

    This C# exercise aims to develop a program that prompts the user for a symbol, a width, and a height, then displays a hollow rectangle of the spe...

  •  Statistics

    This C# exercise aims to develop a program that allows the user to input several numbers and calculate basic statistical operations such as sum, average, mi...

  •  Switch

    This C# exercise aims to develop a program that, given a numerical grade, displays the corresponding text grade according to the following equivalence: 9 and ...

  •  Conditional operator, positive & smaller

    This C# exercise aims to develop a program that asks the user for two numbers and uses the conditional operator (?) to answer the following questions: ...

  •  Prime number

    This C# exercise aims to develop a program that asks the user for an integer and determines if it is a prime number or not. A prime number is one...