Multiplication Table Generator in C++

This exercise focuses on generating the multiplication table of a number provided by the user. It introduces the use of loops in C++, specifically the `for` loop, to repeat a set of operations for a defined number of times.

By completing this program, you will gain experience in:
- Using integer variables
- Handling user input with `cin`
- Iterating with loops to perform repeated calculations
- Outputting formatted results using `cout`

This kind of logic is commonly used in educational software, calculation tools, and logic building in programming exercises. It reinforces understanding of how to apply control flow structures to perform repeated actions.

Group

Introduction to C++

Objective

1. Declare an integer variable to store the number entered by the user.
2. Prompt the user to input the number whose multiplication table will be generated.
3. Use a `for` loop to multiply the number from 1 to 10.
4. In each iteration, display the product in a formatted way.
5. Ensure the output is clear and readable for the user.

Print the multiplication table of a number.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include the iostream library for input and output operations

using namespace std; // Use the standard namespace to avoid using std:: prefix

// Main function - starting point of the program
int main() {
    int number; // Declare an integer variable to store the user input

    // Ask the user to enter a number for which the multiplication table will be generated
    cout << "Enter a number to print its multiplication table: ";
    cin >> number; // Read the input number from the user

    // Use a for loop to print the multiplication table from 1 to 10
    for (int i = 1; i <= 10; ++i) {
        // Display the current step of the multiplication table
        cout << number << " x " << i << " = " << number * i << endl;
    }

    return 0; // Return 0 to indicate the program ended successfully
}

 Output

Enter a number to print its multiplication table: 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

Share this C++ Exercise


More C++ Programming Exercises of Introduction to C++

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++.

  • Check if a Number is Even or Odd in C++

    This exercise teaches you how to determine whether an integer number is even or odd using conditional statements in C++. It’s a fundamental concept that helps beginners understand ...

  • Currency Converter: Euros to US Dollars in C++

    This exercise is designed to help you understand how to work with floating-point numbers and user input/output in C++. You will build a basic currency converter that takes an amoun...

  • Calculate the Perimeter of a Rectangle in C++

    This exercise focuses on computing the perimeter of a rectangle using basic arithmetic operations in C++. It’s a great way for beginners to get hands-on practice with input/output,...

  • Sum of the First N Natural Numbers in C++

    This exercise introduces the concept of summation using a loop in C++. You will create a program that calculates the sum of the first N natural numbers, where N is provided by the ...

  • Print the First 10 Fibonacci Numbers in C++

    This exercise helps you understand how to generate a sequence of numbers based on mathematical logic using iterative programming. Specifically, you'll write a program in C++ that p...

  • Hello World Program in C++

    The "Hello, World!" program is one of the simplest programs to write in any programming language. It serves as an introduction to the syntax and basic functionality of a programmin...