
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Generate Multiplication Table
In this article, we will show you how to write a C++ program to generate the multiplication table of a number. A multiplication table shows how a number is multiplied by 1 to 10 and helps define the multiplication operation for that number. Each row displays the result of multiplying the number by values from 1 to 10.
For example, the multiplication table of 4 looks like this:
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40
In C++, we can generate a multiplication table using different methods. Here are the approaches we'll cover.
- Using a For Loop
- Using a While Loop
- Using a do-While Loop
- Using a Function
- Generating Table for Multiple Numbers(1 to N)
Generate Multiplication Table Using for Loop
In this approach, we use a for loop to print the multiplication table of a number. The loop goes through the numbers 1 to 10, multiplying the given number by each and printing the result.
Example
Here's the complete C++ program to generate a multiplication table using a for loop.
#include <iostream> using namespace std; int main() { int num = 2; // predefined number, for example, 4 // Loop to print the multiplication table of 'num' from 1 to 10 cout<<"The multiplication table for "<< num <<" is as follows:"<< endl; for (int i = 1; i <= 10; ++i) { cout << num << " * " << i << " = " << num * i << endl; } return 0; }
The output below displays the multiplication table for the chosen number from 1 to 10.
The multiplication table for 2 is as follows: 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20
Time Complexity:O(1) because the loop runs a fixed number of times(10).
Space Complexity: O(1) because only a few variables are used.
Generate Multiplication Table Using while Loop
In this approach, we use a while loop to print the multiplication table of a number. The loop keeps running until the counter reaches 10.
Example
Here's the complete C++ program to generate a multiplication table using a while loop.
#include <iostream> using namespace std; int main() { int num = 3; int i = 1; // initialize loop counter cout << "The multiplication table for " << num << " is as follows:" << endl; // Loop to print the multiplication table of 'num' from 1 to 10 while (i <= 10) { cout << num << " * " << i << " = " << num * i << endl; i++; // increment the loop counter } return 0; }
Below is the output of the above program that shows the multiplication table for 3.
The multiplication table for 3 is as follows: 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 3 * 10 = 30
Time Complexity: O(1) because the loop runs a fixed number of times(10).
Space Complexity: O(1) because only a constant amount of space is used.
Generate Multiplication Table Using do-while Loop
In this approach, we use a do-while loop to generate a multiplication table. This type of loop guarantees that the code inside it will run at least once, even if the condition is false from the start.
Example
Here's a complete C++ program that uses a do-while loop to print the multiplication table of 4. It starts at 1, prints the result, increases the counter, and repeats until it reaches 10.
#include <iostream> using namespace std; int main() { int num = 7; int i = 1; // initialize loop counter cout << "The multiplication table for " << num << " is as follows:" << endl; // Do-while loop to print the multiplication table of 'num' from 1 to 10 do { cout << num << " * " << i << " = " << num * i << endl; i++; // increment the loop counter } while (i <= 10); // continue loop until i is greater than 10 return 0; }
The below output shows the multiplication table for 7, generated using a do-while loop.
The multiplication table for 7 is as follows: 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 7 * 10 = 70
Time Complexity: O(1) because the loop runs a fixed number of times(10).
Space Complexity: O(1) because only a constant amount of space is used.
Generate Multiplication Table Using User-defined Function
In this approach, we use a separate function to handle the printing of the multiplication table. This makes the program more modular and reusable.
Example
Here's a complete C++ program where we use a seprate function to generate the multiplication table.
#include <iostream> using namespace std; void printTable(int num) { // Function to print the multiplication table of 'num' from 1 to 10 for (int i = 1; i <= 10; ++i) { cout << num << " * " << i << " = " << num * i << endl; // print the multiplication } } int main() { int num = 9; cout << "The multiplication table for " << num << " is as follows:" << endl; printTable(num); // call function to print the table return 0; }
The output below shows the multiplication table for 9, generated using a separate function.
The multiplication table for 9 is as follows: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90
Time Complexity: O(1).
Space Complexity: O(1).
Generate Multiplication Table from 1 to N
In this approach, we print the multiplication table for multiple numbers. The program will print the table for all numbers from 1 up to a number you specify.
Example
Below is the complete C++ program. If you input a number like 3 or 4, it will print the multiplication tables for numbers 1, 2, 3, and 4.
#include <iostream> using namespace std; int main() { int n = 2; // predefined range for the number of tables (in this case, 2 tables) // Loop to generate multiplication tables for numbers from 1 to n for (int num = 1; num <= n; ++num) { cout << "Multiplication table for " << num << ":\n"; for (int i = 1; i <= 10; ++i) { cout << num << " * " << i << " = " << num * i << endl; } cout << endl; } return 0; }
The output below shows the multiplication tables generated for numbers from 1 to n (in this example, n=2):
Multiplication table for 1: 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 ... 1 * 10 = 10 Multiplication table for 2: 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 ... 2 * 10 = 20
Time Complexity: O(n), where n is the number of multiplication table.
Space Complexity: O(1) because the space used does not depend on the input size. So, constant amount of space is used.