Print multiplication table for a given number
Table of Contents
Introduction
Loops are essential programming constructs that allow repetitive execution of code based on conditions. They help automate repetitive tasks, perform calculations, and develop scalable programs efficiently. In this lesson, we focus on printing a multiplication table using loops. Students will learn how to take input from the user and display a structured multiplication table for any given number. This hands-on lesson reinforces understanding of loops and strengthens problem-solving skills in programming.
Objectives
This lesson aligns with the Outcomes-Based Education (OBE) framework, focusing on four key objectives:
- Understand: Comprehend the concept and structure of loops in programming, including for, while, and do-while loops.
- Learn: Learn how to use loops to automate repetitive tasks, such as printing multiplication tables.
- Practice: Write programs that implement loops to display multiplication tables for user-defined numbers.
- Apply: Apply the knowledge of loops to solve real-world programming problems and optimize repetitive calculations.
By focusing on these objectives, learners will gradually develop the skills to write efficient, reusable, and structured programs.
Beginner-Friendly Source Code
Below is a simple C# example for printing the multiplication table of a given number using a for loop.
using System;
class Program
{
static void Main()
{
// Program to print multiplication table for a given number
Console.Write("Enter a number: ");
int number = int.Parse(Console.ReadLine());
Console.WriteLine($"\nMultiplication Table for {number}:");
for (int i = 1; i <= 10; i++)
{
Console.WriteLine($"{number} x {i} = {number * i}");
}
Console.WriteLine("\nEnd of multiplication table!");
}
}
Instructions:
- Open a C# compiler (Visual Studio, Visual Studio Code, or online compiler).
- encode the code above into a new project.
- Run the program.
- Input any number when prompted.
- Observe the multiplication table displayed from 1 to 10.
Example Output:
Enter a number: 5
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
End of multiplication table!
Summary
In this lesson, students learned to create a multiplication table for any given number using loops. By taking user input and iterating from 1 to 10, learners applied the for loop to perform repetitive calculations efficiently. This lesson reinforced understanding of loop mechanics, user input handling, and structured program output. Mastery of loops in this context is fundamental for tackling more complex programming tasks, including nested loops and automated calculations.
Multiple Choice Questions (Bloom’s Taxonomy)
1. Which loop is commonly used when the number of iterations is known?
A) for
B) while
C) do-while
D) if
2. What will the code Console.WriteLine($"{number} x {i} = {number * i}") output in a multiplication table program?
A) The sum of number and i
B) The product of number and i
C) Only the value of number
D) Only the value of i
3. If the user enters 7, what is the output of the 5th row in the multiplication table?
A) 7 x 4 = 28
B) 7 x 5 = 35
C) 7 x 6 = 42
D) 5 x 7 = 35
4. Which change will print a multiplication table up to 20 instead of 10?
A) Change for(int i = 1; i <= 10; i++) to for(int i = 1; i <= 20; i++)
B) Change int number = int.Parse(Console.ReadLine())
C) Change Console.WriteLine($"{number} x {i} = {number * i}")
D) Change Console.WriteLine("\nEnd of multiplication table!")
5. Which scenario best demonstrates using loops to simplify repetitive tasks?
A) Printing a multiplication table for any user-defined number
B) Writing ten separate Console.WriteLine() statements manually
C) Using if statements to check every number
D) Hardcoding results for one specific number
Suggested Exercises and Assessment
Introduction
Practicing loop-based programs improves problem-solving skills and strengthens programming logic. The following exercises, assessments, and lab tasks help students master loop implementation in real-world scenarios.
Exercises
- Modify the program to print the multiplication table in reverse order (10 down to 1).
- Print a multiplication table for numbers 1 to 5 using nested loops.
- Calculate the sum of all products in the multiplication table for a given number.
- Display the multiplication table in a formatted grid.
Assessment
- Write a program that takes two numbers from the user: the number for the table and the maximum multiplier. Display the multiplication table accordingly.
Lab Exam
- Create a program using a do-while loop that prints the multiplication table for a given number.
- Add input validation to ensure the user enters positive integers.
- Allow the user to repeat the program without restarting it manually.
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.