The Wayback Machine - https://web.archive.org/web/20240826020201/https://www.geeksforgeeks.org/program-to-print-multiplication-table-of-a-number/
Open In App

Program to print multiplication table of a number

Last Updated : 28 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Basic
44.81%
50K

Given a number n as input, 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 :  8
Output : 8 * 1 = 8
         8 * 2 = 16
         8 * 3 = 24
         8 * 4 = 32
         8 * 5 = 40
         8 * 6 = 48
         8 * 7 = 56
         8 * 8 = 64
         8 * 9 = 72
         8 * 10 = 80
         8 * 11 = 88
         8 * 12 = 96

 

Example 1: Display Multiplication table up to 10 
 

C++




// CPP program to print table of a number
#include <iostream>
using namespace std;
 
int main()
{
    int n = 5;  // Change here to change output
    for (int i = 1; i <= 10; ++i)
        cout << n << " * " << i << " = " 
             << n * i << endl;
    
    return 0;
}


Java




// Java program to print table
// of a number
import java.io.*;
 
class table
{  
    // Driver code
    public static void main(String arg[])
    {  
        // Change here to change output
        int n = 5;
         
        for (int i = 1; i <= 10; ++i)
            System.out.println(n + " * " + i +
                               " = " + n * i);
    }
}
 
// This code is contributed by Anant Agarwal.


Python




# Python Program to print table
# of a number upto 10
 
def table(n):
    for i in range (1, 11):
         
        # multiples from 1 to 10
        print "%d * %d = %d" % (n, i, n * i)
 
# number for which table is evaluated
n = 5
table(n)
 
# This article is contributed by Shubham Rana


C#




// C# program to print
// table of a number
using System;
 
class GFG
{
    // Driver code
    public static void Main()
    {
        // Change here to
        // change output
        int n = 5;
         
        for (int i = 1; i <= 10; ++i)
            Console.Write(n + " * " + i +
                              " = " + n *
                               i + "\n");
    }
}
 
// This code is contributed
// by Smitha.


PHP




<?php
// PHP program to print
// table of a number
 
// Driver Code
$n = 5; // Change here to
        // change output
for ($i = 1; $i <= 10; ++$i)
    echo $n , " * " , $i ,
              " = " , $n *
                $i , "\n";
         
// This code is contributed
// by Smitha
?>


Javascript




// JavaScript program to print table of a number
// using recursion
 
 
// 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
function print_table(n, i = 1)
{
    if (i == 11)// base case
        return;
    console.log(n + " * " + i + " = " + n * i);
    i++;//increment i
    print_table(n,i);
}
 
// Driver Code
let n = 5;
print_table(n);
 
// This code is contributed by phasing17


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)

Auxiliary Space: O(1)

This program above computes the multiplication table up to 10 only.
The program below is the modification of above program in which the user is also asked to enter the range up to which multiplication table should be displayed.

Method 2: Using Recursion

C++




// CPP program to print table over a range.
#include <iostream>
using namespace std;
int main()
{
    int n = 8;   // Change here to change input number
    int range = 12; // Change here to change result.
    for (int i = 1; i <= range; ++i)
        cout << n << " * " << i << " = "
            << n * i << endl;
    return 0;
}


Java




// Java program to print table of a number
// using recursion
import java.util.*;
 
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 print_table(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++; // increment i
        print_table(n, i);
    }
 
    public static void main(String[] args)
    {
        int n = 5;
        print_table(n);
    }
}
 
// This code is contributed by phasing17


Python3




# Python3 program to print table of a number
# using recursion
 
# 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
def print_table(n, i=1):
 
    if (i == 11):  # base case
        return
    print(n, "*", i, "=", n * i)
    i += 1  # increment i
    print_table(n, i)
 
# Driver Code
n = 5
print_table(n)
 
# This code is contributed by phasing17


C#




// C# program to print table of a number
// using recursion
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 print_table(int n, int i = 1)
  {
    if (i == 11) // base case
      return;
    Console.WriteLine(n + " * " + i + " = " + n * i);
    i++; // increment i
    print_table(n, i);
  }
  public static void Main(string[] args)
  {
    int n = 5;
    print_table(n);
  }
}
 
// This code is contributed by phasing17


Javascript




// JavaScript program to print table of a number
// using recursion
 
 
// 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
function print_table(n, i = 1)
{
    if (i == 11)// base case
        return;
    console.log(n + " * " + i + " = " + n * i);
    i++;//increment i
    print_table(n,i);
}
 
// Driver Code
let n = 5;
print_table(n);
 
// This code is contributed by phasing17


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)

Auxiliary Space: O(n)

Example 2: Display multiplication table up to a given range 
 

C++




// CPP program to print table over a range.
#include <iostream>
using namespace std;
int main()
{
    int n = 8;   // Change here to change input number
    int range = 12; // Change here to change result.
    for (int i = 1; i <= range; ++i)
        cout << n << " * " << i << " = "
            << n * i << endl;
    return 0;
}


Java




// Java program to print table
// over given range.
import java.io.*;
 
class table
{  
    // Driver code
    public static void main(String arg[])
    {  
        // Change here to change input number
        int n = 8;
         
        // Change here to change result
        int range = 12;
         
        for (int i = 1; i <= range; ++i)
            System.out.println(n + " * " + i
                             + " = " + n * i);
    }
}
 
// This code is contributed by Anant Agarwal.


Python




# Python Program to print table
# of a number given range
 
def table(n, r):
    for i in range (1, r + 1):
         
        # multiples from 1 to r (range)
        print "%d * %d = %d" % (n, i, n * i)
 
# number for which table is evaluated
n = 8
 
# range upto which multiples are to be calculated
r = 12
table(n,r)
 
# This article is contributed by Shubham Rana


C#




// C# program to print
// table over given range.
using System;
 
class GFG
{
    // Driver code
    public static void Main()
    {
        // Change here to
        // change input number
        int n = 8;
         
        // Change here to
        // change result
        int range = 12;
         
        for (int i = 1; i <= range; ++i)
            Console.Write(n + " * " +
                          i + " = " +
                          n * i + "\n");
    }
}
 
// This code is contributed
// by Smitha.


PHP




<?php
// PHP program to print
// table over a range.
 
$n = 8; // Change here to
        // change input number
$range = 12; // Change here to
             // change result.
for ($i = 1; $i <= $range; ++$i)
    echo $n , " * " ,
         $i , " = ",
         $n * $i , "\n";
 
// This code is contributed
// by m_kit
?>


Javascript




<script>
 
// Javascript program to print
// table of a number in range
 
// Driver Code
// Change here to
// change input number
 
let n = 8;
 
// Change here to
// change result.
let range = 12;
for (let i = 1; i <= range; ++i)
    document.write( n + " * " +i +
            " = " + n *
                i +"<br>");
         
// This code is contributed
// by bobby
 
</script>


Output: 

8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
8 * 11 = 88
8 * 12 = 96

Time Complexity: O(n) where n is the range of numbers

Auxiliary Space: O(1)

 



Similar Reads

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 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 8 * 10 = 8
5 min read
Multiplication table till N rows where every Kth row is table of K upto Kth term
Given a number N, the task is to print N rows where every Kth row consists of the multiplication table of K up to Kth term. Examples: Input: N = 3 Output: 1 2 4 3 6 9 Explanation: In the above series, in every Kth row, multiplication table of K upto K terms is printed. Input: N = 5 Output: 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 Approach: The idea is t
5 min read
Kth Smallest Number in Multiplication Table
Given three integers M, N, and K. Consider a grid of M * N, where mat[i][j] = i*j (1 based index). The task is to return the Kth smallest element in the M*N multiplication table. Examples: Input: M = 3, N = 3, K = 5Output: 3Explanation: The 5th smallest element is 3.  Input: M = 2, N = 3, K = 6Output: 6 Explanation: [1, 2, 3][2, 4, 6]. The 6th smal
14 min read
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: n = 3Output: 3Explanation: The 3x3 multiplication
4 min read
Program for scalar multiplication of a matrix
Given a matrix and a scalar element k, our task is to find out the scalar product of that matrix. Examples: Input : mat[][] = {{2, 3} {5, 4}} k = 5 Output : 10 15 25 20 We multiply 5 with every element. Input : 1 2 3 4 5 6 7 8 9 k = 4 Output : 4 8 12 16 20 24 28 32 36 The scalar multiplication of a number k(scalar), multiply it on every entry in th
6 min read
Program for multiplication of array elements
We are given an array, and we have to calculate the product of an array using both iterative and recursive methods. Examples: Input : array[] = {1, 2, 3, 4, 5, 6}Output : 720Here, product of elements = 1*2*3*4*5*6 = 720 Input : array[] = {1, 3, 5, 7, 9}Output : 945 Iterative Method: We initialize result as 1. We traverse array from left to right an
9 min read
Sum of multiplication of triplet of divisors of a number
Given an array arr[] of integers of size n. For each element, you have to print the summation of multiplication of every triplet formed using divisors of this element.Examples: Input: arr[] = {4} Output: 8 4 has three divisors 1, 2 and 4. 1 * 2 * 4 = 8Input: arr[] = {9, 5, 6} Output: 27 0 72 9 has three divisors 1, 3 and 9. 1 * 3 * 9 = 27 5 has two
13 min read
Find minimum number K such that sum of array after multiplication by K exceed S
Given an array arr[] of N elements and an integer S, the task is to find the minimum number K such that the sum of the array elements does not exceed S after multiplying all the elements by K.Examples: Input: arr[] = { 1 }, S = 50 Output: 51 Explanation: The sum of array elements is 1. Now the multiplication of 1 with 51 gives 51 which is &gt; 50.
4 min read
Minimum division by 10 and multiplication by 2 required to reduce given number to 1
Given an integer N, the task is to reduce N to 1 by a minimum number of operations involving multiplication by 2 and division by 10. If 1 cannot be obtained, then print "-1". Examples: Input: N = 5Output: 2Explanation:Below are the operations performed:1st operation: Multiply N by 2. Therefore, N = 5 * 2 = 10.2nd operation: Divide N by 10. Therefor
5 min read
Factorial of a number without using multiplication
Given a positive number N, the task is to calculate the factorial of N without using the multiplication operator. Examples: Input: N = 5Output:120Explanation:5*4*3*2*1=120 Input: N = 7Output:5040 Observation: A*B=A+A+A+A...B times. This observation can be used as follows: 5!=5*4*3*2*1 =(5+5+5+5)*3*2*1 =(20+20+20)*2*1 =60+60 =120 Approach 1: The pro
7 min read
Maximum number of multiplication by 3 or division by 2 operations possible on an array
Given an array arr[] consisting of N positive integers, the task is to find the maximum number of times each array element can either be multiplied by M or divided by K. Note: At least one element needs to be divided by M and K respectively in each operation. Examples: Input: arr[] = {5, 2, 4}, M = 3, K = 2Output: 3Explanation:One possible way to p
6 min read
Find minimum value expression by inserting addition or multiplication operator between digits of given number
Given string str that contains only digits, the task is to return an expression by inserting the '+' or '*' operator between every two digits such that the arithmetic value of the expression is minimized. Example: Input: str = "322"Output: "3+2*2"Explanation: The value of above expression is 7 which is minimum possible over the other expressions "3
6 min read
Multiply a number with 10 without using multiplication operator
Given a number, the task is to multiply it with 10 without using multiplication operator?Examples: Input : n = 50 Output: 500 // multiplication of 50 with 10 is = 500 Input : n = 16 Output: 160 // multiplication of 16 with 10 is = 160 A simple solution for this problem is to run a loop and add n with itself 10 times. Here we need to perform 10 oper
5 min read
Multiply two integers without using multiplication, division and bitwise operators, and no loops
By making use of recursion, we can multiply two integers with the given constraints. To multiply x and y, recursively add x y times. Approach: Since we cannot use any of the given symbols, the only way left is to use recursion, with the fact that x is to be added to x y times. Base case: When the numbers of times x has to be added becomes 0. Recurs
9 min read
Write you own Power without using multiplication(*) and division(/) operators
Method 1 (Using Nested Loops): We can calculate power by using repeated addition. For example to calculate 5^6. 1) First 5 times add 5, we get 25. (5^2) 2) Then 5 times add 25, we get 125. (5^3) 3) Then 5 times add 125, we get 625 (5^4) 4) Then 5 times add 625, we get 3125 (5^5) 5) Then 5 times add 3125, we get 15625 (5^6) C/C++ Code // C++ code fo
13 min read
Check for integer overflow on multiplication
Given two integer a and b, find whether their product (a x b) exceed the signed 64 bit integer or not. If it exceed print Yes else print No. Examples: Input : a = 100, b = 200 Output : No Input : a = 10000000000, b = -10000000000Output : Yes Approach : If either of the number is 0, then it will never exceed the range.Else if the product of the two
4 min read
Fast Fourier Transformation for polynomial multiplication
Given two polynomial A(x) and B(x), find the product C(x) = A(x)*B(x). There is already an O([Tex]n^2 [/Tex]) naive approach to solve this problem. here. This approach uses the coefficient form of the polynomial to calculate the product.A coefficient representation of a polynomial [Tex]A(x)=\sum_{j=0}^{n-1}a_jx^j [/Tex]is a = a0, a1, ..., an-1. Exa
13 min read
Iterative Fast Fourier Transformation for polynomial multiplication
Given two polynomials, A(x) and B(x), find the product C(x) = A(x)*B(x). In the previous post, we discussed the recursive approach to solve this problem which has O(nlogn) complexity. Examples: Input : A[] = {9, -10, 7, 6} B[] = {-5, 4, 0, -2} Output : C(x) = [Tex]-12x^6 -14x^5 +44x^4 -20x^3 - 75x^2 +86x -45[/Tex] In real-life applications such as
12 min read
Multiplication with a power of 2
Given two numbers x and n, we need to multiply x with 2nExamples : Input : x = 25, n = 3 Output : 200 25 multiplied by 2 raised to power 3 is 200. Input : x = 70, n = 2 Output : 280 A simple solution is to compute n-th power of 2 and then multiply with x. C/C++ Code // Simple C/C++ program // to compute x * (2^n) #include &lt;bits/stdc++.h&gt; usin
5 min read
Booth’s Multiplication Algorithm
Booth's algorithm is a multiplication algorithm that multiplies two signed binary numbers in 2's complement notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed. Booth’s algorithm is of interest in the study of computer architecture. Here's the implementation of the algorit
15 min read
Divide two integers without using multiplication, division and mod operator | Set2
Given two integers say a and b. Find the quotient after dividing a by b without using multiplication, division and mod operator.Examples: Input: a = 10, b = 3Output: 3 Input: a = 43, b = -8Output: -5 This problem has been already discussed here. In this post, a different approach is discussed.Approach : Let a/b = c.Take log on both sideslog(a) - lo
7 min read
Multiplication of two polynomials using Linked list
Given two polynomials in the form of linked list. The task is to find the multiplication of both polynomials. Examples: Input: Poly1: 3x^2 + 5x^1 + 6, Poly2: 6x^1 + 8 Output: 18x^3 + 54x^2 + 76x^1 + 48 On multiplying each element of 1st polynomial with elements of 2nd polynomial, we get 18x^3 + 24x^2 + 30x^2 + 40x^1 + 36x^1 + 48 On adding values wi
15 min read
Right most non-zero digit in multiplication of array elements
Given an array arr[] of N non-negative integers. The task is to find the rightmost non-zero digit in the product of array elements.Examples: Input: arr[] = {3, 5, 6, 90909009} Output: 7Input: arr[] = {7, 42, 11, 64} Output: 6 Result of multiplication is 206976 So the rightmost digit is 6 Approach: The question is too simple if you know basic maths.
8 min read
Multiplication on Array : Range update query in O(1)
Consider an array A[] of integers and the following two types of queries. update(l, r, x): multiply x to all values from A[l] to A[r] (both inclusive).printArray(): Prints the current modified array. Examples: Input: A[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} update(0, 2, 2) update(1, 4, 3) print() update(4, 8, 5) print() Output: 2 6 6 3 15 5 5 5 5 1 Exp
7 min read
Minimum steps to convert X to Y by repeated division and multiplication
Given two integers X and Y, the task is to find the minimum number of steps to convert integer X to Y using any of the operations in each step: Divide the number by any natural numberMultiply the number with any natural number Examples: Input: X = 8, Y = 12 Output: 2 Explanation: First divide 8 by 2: 8/2 = 4 Then multiply by 3: 4*3 = 12 Input: X =
7 min read
Product of Complex Numbers using three Multiplication Operation
Given four integers a, b, c, and d which represents two complex numbers of the form (a + bi) and (c + di), the task is to find the product of the given complex numbers using only three multiplication operations.Examples: Input: a = 2, b = 3, c = 4 and d = 5 Output: -7 + 22i Explanation: Product is given by: (2 + 3i)*(4 + 5i) = 2*4 + 4*3i + 2*5i + 3
6 min read
Smallest string without any multiplication sign that represents the product of two given numbers
Given two numbers A and B, the task is to print the string of the smallest possible length which evaluates to the product of the two given numbers i.e., A*B, without using the multiplication sign. Any perfect power of 2 can be expressed in the form of a left-shift operator. For Example: N = 2 = 1&lt;&lt;1 = "&lt;&lt;1" N = 4 = 1&lt;&lt;2 = "&lt;
9 min read
Minimize subset addition or multiplication to make two given triplets equal
Given three integers A, B, C denoting a triplet and three integers P, Q, R denoting another triplet. Repeatedly select any integer and add or multiply it to all elements in a subset (A, B, C) with that integer until the two given triplets become equal. The task is to find the minimum number of such operations required to make the two triplets equal
14 min read
12 hour clock Multiplication
Given two positive integers num1 and num2, the task is to find the product of the two numbers on a 12-hour clock rather than a number line. Note: Assume the Clock starts from 0 hours to 11 hours. Examples: Input: Num1 = 3, Num2 = 7Output: 9Explanation: 3*7 = 21. The time in a 12 hour clock is 9. Input: Num1 = 3, Num2 = 4Output: 0 Approach: Follow t
3 min read
Minimize subsequence multiplication to convert Array sum to be power of 2
Given an array A[] of size N such that every element is a positive power of 2. In one operation, choose a non-empty subsequence of the array and multiply all the elements of the subsequence with any positive integer such that the sum of the array becomes a power of 2. The task is to minimize this operation. Examples: Input: S = {4, 8, 4, 32} Output
6 min read
Practice Tags :