Exercise
Multiplication table (use while)
Objetive
Write a C# program that prompts the user to enter a number and displays its multiplication table using a 'while' loop.
Example Code
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
int number; // Declaring a variable to store the number entered by the user
int counter = 1; // Declaring and initializing the counter variable to 1
// Asking the user to enter a number for the multiplication table
Console.Write("Please enter a number: ");
number = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Displaying the multiplication table using a while loop
Console.WriteLine("The multiplication table for {0} is:", number); // Printing the header for the table
// Using a while loop to display the multiplication table from 1 to 10
while (counter <= 10) // The loop continues as long as the counter is less than or equal to 10
{
// Printing the result of the multiplication
Console.WriteLine("{0} x {1} = {2}", number, counter, number * counter); // Printing the current multiplication result
counter++; // Incrementing the counter by 1 after each iteration
}
}
}