Group
C# Flow Control Basics
Objective
Write a C# program that display multiplication tables from 2 to 6 using nested "do...while" loops.
Example C# Exercise
Show C# Code
// First and Last Name: John Doe
using System;
namespace MultiplicationTables
{
class Program
{
static void Main(string[] args)
{
// Declare variables for the multiplication number (from 2 to 6) and multiplier (from 1 to 10)
int num = 2; // Start from number 2 for multiplication
// Outer loop to iterate through numbers 2 to 6
do
{
int multiplier = 1; // Start from 1 for multiplication
// Print the heading for the current multiplication table
Console.WriteLine("Multiplication Table for " + num + ":");
// Inner loop to multiply the current number by multipliers from 1 to 10
do
{
// Display the current multiplication result
Console.WriteLine(num + " x " + multiplier + " = " + (num * multiplier));
multiplier++; // Increment the multiplier
} while (multiplier <= 10); // Continue the loop while multiplier is less than or equal to 10
Console.WriteLine(); // Print an empty line for better readability between tables
num++; // Increment the number to print the next table
} while (num <= 6); // Continue the loop while the number is less than or equal to 6
}
}
}
Output
Multiplication Table for 2:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Multiplication Table for 3:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Multiplication Table for 4:
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
Multiplication Table for 5:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Multiplication Table for 6:
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60