Table of contents
1.
Introduction
2.
What is a Multiplication Table?
3.
Program to Print Multiplication Table in C
3.1.
1. Using loops & without storing them in an array
3.2.
C
3.3.
2. Using loops &  a 2-D array
3.4.
C
4.
Frequently Asked Questions
4.1.
Can I modify the code to display a multiplication table for numbers beyond 10?
4.2.
Why use a 2-D array for a multiplication table?
4.3.
What are the benefits of using loops without arrays for this program?
5.
Conclusion 
Last Updated: Oct 7, 2024
Easy

C Program to Generate Multiplication Table

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A multiplication table is a simple tool that helps children learn & remember the results of multiplying numbers. It's a grid that shows the products of two numbers, usually from 1 to 10 or 12. It helps kids develop their understanding of multiplication & sets the foundation for more advanced math concepts. This concept is not just useful for kids but a good basic example for new programmers also. 

C Program to Generate Multiplication Table

In this article, we'll learn how to create a program in C that generates multiplication tables. We'll look at two different methods: one using loops without storing the results in an array, & another using loops with a 2-D array. 

What is a Multiplication Table?

The multiplication table, also known as the times table, is a basic math tool that shows the products of two numbers. It's usually presented as a grid, with one number along the top row & another down the leftmost column. The intersection of each row & column contains the product of the two numbers.

For example, in a 10x10 multiplication table, the top row would have numbers 1 through 10, & the leftmost column would also have numbers 1 through 10. The cell where the row for 3 & the column for 4 intersect would contain the number 12, which is the product of 3 & 4.

Learning multiplication tables is a crucial part of elementary math education. It helps children develop fluency in multiplication, which is essential for more advanced math topics like division, fractions, & algebra. By memorizing the multiplication tables, students can quickly recall the answers to basic multiplication problems without having to calculate each time.

Program to Print Multiplication Table in C

Now that we understand what a multiplication table is & why it's important, let's create a program in C to generate these tables.

We'll start by looking at the basic structure of the program. We'll need to use nested loops to iterate through the rows & columns of the table. The outer loop will handle the rows, while the inner loop will handle the columns.

Here's a simple example of what the loop structure might look like:

for (int i = 1; i <= 10; i++) {
    for (int j = 1; j <= 10; j++) {
        // Print the product of i and j
    }
    // Move to the next row
}


In this example, we use two for loops. The outer loop uses the variable i to iterate from 1 to 10, representing the rows. The inner loop uses the variable j to iterate from 1 to 10, representing the columns.

Inside the inner loop, we'll need to print the product of i & j. After the inner loop finishes, we'll move to the next row.

In the following sections, we'll look at two specific ways to implement this:

  • Using loops & without storing the results in an array
     
  • Using loops & a 2-D array

 

1. Using loops & without storing them in an array


In this method, we'll use nested loops to generate the multiplication table on the fly, without storing the results in an array. Let’s see the code:

  • C

C

#include <stdio.h>

int main() {

   int n = 10; // The size of the multiplication table  

   for(int i = 1; i <= n; i++) {

       for(int j = 1; j <= n; j++) {

           printf("%d\t", i * j);

       }

       printf("\n");

   } 

   return 0;

}
You can also try this code with Online C Compiler
Run Code


Output

1 2 3 4 5 6 7 8 9 10 
2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50 
6 12 18 24 30 36 42 48 54 60 
7 14 21 28 35 42 49 56 63 70 
8 16 24 32 40 48 56 64 72 80 
9 18 27 36 45 54 63 72 81 90 
10 20 30 40 50 60 70 80 90 100


In this code : 
 

  1. We include the stdio.h header for input/output operations.
     
  2. In the main() function, we declare a variable n to represent the size of the multiplication table. In this case, we set it to 10 for a 10x10 table.
     
  3. We start the outer loop, which iterates i from 1 to n. This represents the rows of the table.
     
  4. Inside the outer loop, we start the inner loop, which iterates j from 1 to n. This represents the columns of the table.
     
  5. Inside the inner loop, we use printf() to print the product of i & j. We use %d as the format specifier for integers & \t for a tab character to create space between the numbers.
     
  6. After the inner loop finishes, we use printf("\n") to move to the next line, starting a new row.
     
  7. The outer loop continues until all rows are printed.
     

Note: This method directly calculates & prints the product of each pair of numbers without storing them. It's a straightforward approach that's easy to understand.

2. Using loops &  a 2-D array

In this method, we'll use nested loops to fill a 2-D array with the multiplication results. Once populated, the table is printed by accessing the array values. Let’s see the code:

  • C

C

#include <stdio.h>

int main() {
int rows, cols;

// Define the size of the table
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

// Declare a 2D array to store the multiplication table
int table[rows][cols];

// Fill the 2D array using loops
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
table[i-1][j-1] = i * j; // Store the result of multiplication
}
}

// Print the multiplication table
printf("\nMultiplication Table:\n");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
printf("%d\t", table[i-1][j-1]);
}
printf("\n");
}

return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Enter the number of rows: 5
Enter the number of columns: 5
Multiplication Table:
1       2       3       4       5
2       4       6       8       10
3       6       9       12      15
4       8       12      16      20
5       10      15      20      25

 

In this code :

  • The program takes the number of rows and columns as input from the user.
     
  • It uses two nested loops to populate a 2D array with the products of row and column indices.
     
  • Another set of nested loops is used to print the multiplication table stored in the 2D array.

Frequently Asked Questions

Can I modify the code to display a multiplication table for numbers beyond 10?

Yes, you can adjust the loop conditions in the code to accommodate any range of numbers. Just ensure your array size matches the range you intend to display.

Why use a 2-D array for a multiplication table?

A 2-D array helps organize the data in a structured format, making it easier to access specific multiplication results and perform further operations like transposition or searching within the table.

What are the benefits of using loops without arrays for this program?

Using loops without arrays reduces memory usage, as no additional storage space is required. It's efficient for simple displays and when you don't need to retain the results after display.

Conclusion 

In this article, we learned about multiplication tables & their importance in math education. We explained how to create a program in C to generate multiplication tables using loops. We looked at two methods: one without storing the results in an array & another using a 2-D array. We discussed the algorithms used in each method & analyzed their time & space complexity. 

Live masterclass