Table of contents

C++ Program to Generate Multiplication Table

Write a C++ program to generate a multiplication table for a number entered by the user, up to a given limit N. For example, if the user inputs the number 3 and the limit N as 10, the program should print the multiplication table for 3 from 3x1 to 3x10.

Input and Output Examples

Example 1:

  • Input: Number = 3, Limit = 5
  • Output:
    • 3 x 1 = 3
    • 3 x 2 = 6
    • 3 x 3 = 9
    • 3 x 4 = 12
    • 3 x 5 = 15

Example 2:

  • Input: Number = 5, Limit = 3
  • Output:
    • 5 x 1 = 5
    • 5 x 2 = 10
    • 5 x 3 = 15

Algorithm to Generate Multiplication Table

  1. Start the program.
  2. Prompt the user to enter the base number for which the table is to be generated.
  3. Prompt the user to enter the limit N up to which the multiplication should be printed.
  4. Use a loop to iterate from 1 to N.
  5. In each iteration, multiply the base number with the loop counter and print the result.
  6. End the program.

Below is the C++ program to generate multiplication tables:

cpp
#include <iostream> 
using namespace std; 

int main() {
    // Declare variables to store the base number and limit
    int number, limit;
    
    // Prompt the user to enter the base number
    cout << "Enter the number for the multiplication table: ";
    cin >> number;

    // Prompt the user to enter the limit up to which the table should go
    cout << "Enter the limit up to which you want the table: ";
    cin >> limit;

    // Use a for loop to generate and display the multiplication table
    for (int i = 1; i <= limit; ++i) {
        // Print each line of the table
        cout << number << " x " << i << " = " << number * i << endl;
    }

    // Ending the program
    return 0;
}

Testing the Program with Different Input Values

To ensure the program functions as expected, test it with various numbers and limits:

cpp
Input: Number = 10, Limit = 10
Output:
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

Programming

Related Articles