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 = 50Input: 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.
// 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;
}
#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 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 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# 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 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 fromi = 1toi = 10.First Iteration (
i = 1):
- The loop multiplies
n = 5byi = 1.- Result:
5 * 1 = 5.- Output:
5 * 1 = 5.Second Iteration (
i = 2):
- The loop multiplies
n = 5byi = 2.- Result:
5 * 2 = 10.- Output:
5 * 2 = 10.Third Iteration (
i = 3):
- The loop multiplies
n = 5byi = 3.- Result:
5 * 3 = 15.- Output:
5 * 3 = 15.....
....Tenth Iteration (
i = 10):
- The loop multiplies
n = 5byi = 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.
#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);
}
#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;
}
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);
}
}
# 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)
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);
}
}
// 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);
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)