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
C# Exercise Example
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}");
}
}
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#.
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...
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...
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...
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 ...
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: ...
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...