Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Multiplication Table
3.
Program to Print Multiplication Table in C
3.1.
Using loops & without storing them in an array
3.2.
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: Jun 17, 2024
Easy

C Program to Generate Multiplication Table

Author Sinki Kumari
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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. 

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

 

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;

}

Output

output

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.

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. 

You can refer to our guided paths on Code 360. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Previous article
Factorial Program in C
Next article
Sum of Digits of a Number in C
Live masterclass