Program for multiplication table
Last Updated :
09 Jul, 2025
Given a number n, we need to print its table.
Examples :
Input: 5
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
Input: 2
Output:
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
Iterative Approach
The iterative approach for printing a multiplication table involves using a loop to calculate and print the product of a given number and the numbers in range from 1 to 10. In this method, you begin with the number whose table you want to print and use a loop to multiply it with increasing values.
C++
// CPP program to print table of a number
#include <iostream>
using namespace std;
void printTable(int n) {
for (int i = 1; i <= 10; ++i)
cout << n << " * " << i << " = "
<< n * i << endl;
}
int main() {
int n = 5;
printTable(n);
return 0;
}
C
#include <stdio.h>
void printTable(int n) {
for (int i = 1; i <= 10; ++i)
printf("%d * %d = %d\n", n, i, n * i);
}
int main() {
int n = 5;
printTable(n);
return 0;
}
Java
// Java program to print table of a number
import java.io.*;
class GfG {
public static void printTable(int n) {
for (int i = 1; i <= 10; ++i)
System.out.println(n + " * " + i +
" = " + n * i);
}
public static void main(String arg[]){
int n = 5;
printTable(n);
}
}
Python
# Python Program to print table of a number
def printTable(n):
for i in range (1, 11):
# multiples from 1 to 10
print ("%d * %d = %d" % (n, i, n * i))
if __name__ == "__main__":
n = 5
printTable(n)
C#
// C# program to print table of a number
using System;
class GfG {
public static void printTable(int n) {
for (int i = 1; i <= 10; ++i)
Console.Write(n + " * " + i +
" = " + n *
i + "\n");
}
public static void Main() {
int n = 5;
printTable(n);
}
}
JavaScript
// Javascript program to print
// table of a number
function printTable(n) {
for (let i = 1; i <= 10; ++i)
console.log( n + " * " +i +
" = " + n *
i);
}
// Driver Code
let n = 5;
printTable(n);
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(1)
Space Complexity - O(1)
Illustration
Step by step execution of loop for the multiplication table of n = 5.
We have n = 5
, and the loop will iterate from i = 1
to i = 10
.
First Iteration (i = 1
):
- The loop multiplies
n = 5
by i = 1
. - Result:
5 * 1 = 5
. - Output:
5 * 1 = 5
.
Second Iteration (i = 2
):
- The loop multiplies
n = 5
by i = 2
. - Result:
5 * 2 = 10
. - Output:
5 * 2 = 10
.
Third Iteration (i = 3
):
- The loop multiplies
n = 5
by i = 3
. - Result:
5 * 3 = 15
. - Output:
5 * 3 = 15
.
....
....
Tenth Iteration (i = 10
):
- The loop multiplies
n = 5
by i = 10
. - Result:
5 * 10 = 50
. - Output:
5 * 10 = 50
.
Recursive Approach
In this method, we pass i as an additional parameter with initial value as 1. We print n * i and then recursively call for i+1. We stop the recursion when i becomes 11 as we need to print only 10 multiples of given number and i.
C++
#include <iostream>
using namespace std;
// printTable() prints table of number and takes
// 1 required value that is number of whose teble
// to be printed and an optional input i whose d
// efault value is 1
void printTable(int n, int i = 1)
{
if (i == 11)
return;
cout << n << " * " << i << " = " << n * i << endl;
i++;
printTable(n, i);
}
int main()
{
int n = 5;
printTable(n);
}
C
#include <stdio.h>
// printTable() prints table of number and takes
// 1 required value that is number of whose table
// to be printed and an optional input i whose default value is 1
void printTable(int n, int i) {
if (i == 11)
return;
printf("%d * %d = %d\n", n, i, n * i);
i++;
printTable(n, i);
}
int main() {
int n = 5;
printTable(n, 1);
return 0;
}
Java
import java.util.*;
class GfG {
// printTable() prints table of number and takes
// 1 required value that is number of whose teble to be
// printed and an optional input i whose default value is 1
static void printTable(int n, Integer... val) {
int i = 1;
if (val.length != 0)
i = val[0];
if (i == 11) // base case
return;
System.out.println(n + " * " + i + " = " + n * i);
i++;
printTable(n, i);
}
public static void main(String[] args) {
int n = 5;
printTable(n);
}
}
Python
# printTable() prints table of number and takes
# 1 required value that is number of whose teble to be printed
# and an optional input i whose default value is 1
def printTable(n, i=1):
if (i == 11): # base case
return
print(n, "*", i, "=", n * i)
i += 1
printTable(n, i)
if __name__ == "__main__":
n = 5
printTable(n)
C#
using System;
using System.Collections.Generic;
class GfG {
// print_table() prints table of number and takes
// 1 required value that is number of whose teble to be
// printed and an optional input i whose default value is 1
static void printTable(int n, int i = 1) {
if (i == 11) // base case
return;
Console.WriteLine(n + " * " + i + " = " + n * i);
i++;
printTable(n, i);
}
public static void Main(string[] args) {
int n = 5;
printTable(n);
}
}
JavaScript
// printTable() prints table of number and takes
//1 required value that is number of whose teble to be printed
//and an optional input i whose default value is 1
function printTable(n, i = 1) {
if (i == 11)// base case
return;
console.log(n + " * " + i + " = " + n * i);
i++;
printTable(n,i);
}
// Driver Code
let n = 5;
printTable(n);
Output5 * 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(1)
Space Complexity - O(1)
Similar Reads
CSES Solutions - Multiplication Table Find the middle element when the numbers in an n à n multiplication table are sorted in increasing order. It is assumed that n is odd. For example, the 3 à 3 multiplication table is as follows: 1 2 32 4 63 6 9 The numbers in increasing order are [1,2,2,3,3,4,6,6,9], so the answer is 3 Example:Input:
4 min read
Recursive Program to print multiplication table of a number Given a number N, the task is to print its multiplication table using recursion. Examples Input: N = 5 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 Input: N = 8 Output: 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 *
5 min read
Recursive Program to print multiplication table of a number Given a number N, the task is to print its multiplication table using recursion. Examples Input: N = 5 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 Input: N = 8 Output: 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 *
5 min read
Recursive Program to print multiplication table of a number Given a number N, the task is to print its multiplication table using recursion. Examples Input: N = 5 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 Input: N = 8 Output: 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 *
5 min read
Matrix Multiplication | Recursive Given two matrices A and B. The task is to multiply matrix A and matrix B recursively. If matrix A and matrix B are not multiplicative compatible, then generate output "Not Possible". Examples : Input: A = 12 56 45 78 B = 2 6 5 8Output: 304 520 480 894Input: A = 1 2 3 4 5 6 7 8 9 B = 1 2 3 4 5 6 7 8
9 min read
Printing brackets in Matrix Chain Multiplication Problem Given an array arr[] which represents the chain of matrices such that the dimensions of the ith matrix are arr[i-1] x arr[i]. The task is to find the correct parenthesis of the matrices such that when we multiply all the matrices together, the cost or total number of element multiplications is minim
15+ min read