VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-generate-multiplication-table/

⇱ C Program to Generate Multiplication Table - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program to Generate Multiplication Table

Last Updated : 27 Mar, 2023

In this article, we are creating a multiplication table in c which is a basic program for printing tables in c. We are printing multiplication tables of the number up to a given range. We will use the concepts of looping and using a 2-D array to print a Multiplication Table. 

Multiplication Table

A multiplication table is a table that shows the multiples of a number. A multiplication table is created by multiplying a constant number from 1 to a given range of numbers in repetition order. 

Input: 

num = 5 
range = 10

Output:

5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Program to Print Multiplication Table in C

There are two approaches for printing tables in c

  1. Using loops and without storing them in an array
  2. Using loops and a 2-D array

1. Using loops and without storing them in an array 

The idea is to use the concept of looping and directly print the multiplication table without storing them in an array.

Algorithm:

  • Take the input of the number and the range of the multiplication table.
  • Declare a variable to store the product.
  • Use a for loop to directly multiply and print the Multiplication table.

Output
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
  • Time Complexity: O(n), as only for loop is required.
  • Auxiliary Space: O(1), no extra space is required, so it is a constant.

2. Using loops and a 2-D array

Algorithm:

  • Take the input of the number and the range of the multiplication table.
  • Now use a for loop(variable "k")is used to traverse the 2-D array, it iterates through 0 to the range.
  • The first column stores the number (i.e arr[k][0] = num) .
  • The second column stores the value to be multiplied (i.e arr[k][1] = k+1) .
  • The third column stores the product (i.e arr[k][2] = arr[k][1] * arr[k][0] ) .
  • Then use another loop to print the multiplication table.

Output
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Time Complexity: O(n).

Auxiliary Space: O(row *col), as a  2-D array is used.

Comment
Article Tags: