Posted on by Kalkicode
Code Mathematics

# Generate multiplication table of given number

Generating a multiplication table of a given number is a basic mathematical operation that helps in understanding the multiplication relationships between the given number and integers from 1 to a specified limit. This article explains how to generate a multiplication table of a given number using a C program.

## Problem Statement

Given an integer 'number', the task is to generate the multiplication table for that number, displaying the product of the number and integers from 1 to 10.

## Example

For instance, if the given number is 7, the multiplication table for 7 would be:

``````7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
...
7 X 10 = 70``````

Similarly, for the number 5:

``````5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
...
5 X 10 = 50``````

And for the number 11:

``````11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
...
11 X 10 = 110``````

## Idea to Solve

To solve this problem, you need to iterate through integers from 1 to 10 and calculate the product of the given number and each integer. Then, print the formatted output of the multiplication table.

## Pseudocode

``````function table_of_number(number):
if number <= 0:
return
Print "Number", number
Loop from i = 1 to 10:
Calculate the result = number * i
Print the multiplication expression and result

function main():
table_of_number(7)
table_of_number(5)
table_of_number(11)``````

## Algorithm Explanation

1. The `table_of_number` function takes one parameter: 'number'.
2. It checks if the 'number' is less than or equal to 0, and if so, it returns without performing any calculations.
3. It prints "Number" followed by the given 'number'.
4. It then initializes a loop from 'i' = 1 to 10.
5. Inside the loop, it calculates the result of the multiplication (result = number * i).
6. It prints the formatted multiplication expression using the given 'number', 'i', and the calculated 'result'.
7. The `main` function tests the `table_of_number` function with different values.

## Code Solution

``````/*
C Program
Generate multiplication table of given number
*/
#include <stdio.h>

//Display tables of a given number
void table_of_number(int number)
{
if (number <= 0)
{
return;
}
int result = 0;
printf("\nNumber %d\n", number);
//display table result from 1 to 10
for (int i = 1; i <= 10; ++i)
{
//Multiply the given number by i value
result = (number * i);
//Display result
printf("%d X %d =  %d\n", number, i, result);
}
}
int main()
{
//Simple Case
table_of_number(7);
table_of_number(5);
table_of_number(11);
return 0;
}``````

#### Output

``````Number 7
7 X 1 =  7
7 X 2 =  14
7 X 3 =  21
7 X 4 =  28
7 X 5 =  35
7 X 6 =  42
7 X 7 =  49
7 X 8 =  56
7 X 9 =  63
7 X 10 =  70

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

Number 11
11 X 1 =  11
11 X 2 =  22
11 X 3 =  33
11 X 4 =  44
11 X 5 =  55
11 X 6 =  66
11 X 7 =  77
11 X 8 =  88
11 X 9 =  99
11 X 10 =  110``````
``````// Java Program
// Generate multiplication table of given number
class MyMath
{
//Display tables of a given number
public void table_of_number(int number)
{
if (number <= 0)
{
return;
}
int result = 0;
System.out.print("\nNumber " + number + "\n");
//display table result from 1 to 10
for (int i = 1; i <= 10; ++i)
{
//Multiply the given number by i value
result = (number * i);
System.out.print("" + number + " X " + i + " = " + result + "\n");
}
}
public static void main(String[] args)
{
MyMath obj = new MyMath();
//Simple Case
obj.table_of_number(7);
obj.table_of_number(5);
obj.table_of_number(11);
}
}``````

#### Output

``````Number 7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70

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

Number 11
11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
11 X 4 = 44
11 X 5 = 55
11 X 6 = 66
11 X 7 = 77
11 X 8 = 88
11 X 9 = 99
11 X 10 = 110``````
``````// C++ Program
// Generate multiplication table of given number
#include<iostream>

using namespace std;
class MyMath
{
public:
//Display tables of a given number
void table_of_number(int number)
{
if (number <= 0)
{
return;
}
int result = 0;
cout << "\nNumber " << number << "\n";
//display table result from 1 to 10
for (int i = 1; i <= 10; ++i)
{
//Multiply the given number by i value
result = (number * i);
cout << "" << number << " X " << i << " = " << result << "\n";
}
}
};
int main()
{
MyMath obj ;
//Simple Case
obj.table_of_number(7);
obj.table_of_number(5);
obj.table_of_number(11);
return 0;
}``````

#### Output

``````Number 7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70

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

Number 11
11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
11 X 4 = 44
11 X 5 = 55
11 X 6 = 66
11 X 7 = 77
11 X 8 = 88
11 X 9 = 99
11 X 10 = 110``````
``````// C# Program
// Generate multiplication table of given number
using System;
class MyMath
{
//Display tables of a given number
public void table_of_number(int number)
{
if (number <= 0)
{
return;
}
int result = 0;
Console.Write("\nNumber " + number + "\n");
//display table result from 1 to 10
for (int i = 1; i <= 10; ++i)
{
//Multiply the given number by i value
result = (number * i);
Console.Write("" + number + " X " + i + " = " + result + "\n");
}
}
public static void Main(String[] args)
{
MyMath obj = new MyMath();
//Simple Case
obj.table_of_number(7);
obj.table_of_number(5);
obj.table_of_number(11);
}
}``````

#### Output

``````Number 7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70

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

Number 11
11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
11 X 4 = 44
11 X 5 = 55
11 X 6 = 66
11 X 7 = 77
11 X 8 = 88
11 X 9 = 99
11 X 10 = 110``````
``````<?php
// Php Program
// Generate multiplication table of given number
class MyMath
{
//Display tables of a given number
public	function table_of_number(\$number)
{
if (\$number <= 0)
{
return;
}
\$result = 0;
echo "\nNumber ". \$number ."\n";
//display table result from 1 to 10
for (\$i = 1; \$i <= 10; ++\$i)
{
//Multiply the given number by i value
\$result = (\$number * \$i);
echo "". \$number ." X ". \$i ." = ". \$result ."\n";
}
}
}

function main()
{
\$obj = new MyMath();
//Simple Case
\$obj->table_of_number(7);
\$obj->table_of_number(5);
\$obj->table_of_number(11);
}
main();``````

#### Output

``````Number 7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70

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

Number 11
11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
11 X 4 = 44
11 X 5 = 55
11 X 6 = 66
11 X 7 = 77
11 X 8 = 88
11 X 9 = 99
11 X 10 = 110``````
``````// Node Js Program
// Generate multiplication table of given number
class MyMath
{
//Display tables of a given number
table_of_number(number)
{
if (number <= 0)
{
return;
}
var result = 0;
process.stdout.write("\nNumber " + number + "\n");
//display table result from 1 to 10
for (var i = 1; i <= 10; ++i)
{
//Multiply the given number by i value
result = (number * i);
process.stdout.write("" + number + " X " + i + " = " + result + "\n");
}
}
}

function main()
{
var obj = new MyMath();
//Simple Case
obj.table_of_number(7);
obj.table_of_number(5);
obj.table_of_number(11);
}
main();``````

#### Output

``````Number 7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70

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

Number 11
11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
11 X 4 = 44
11 X 5 = 55
11 X 6 = 66
11 X 7 = 77
11 X 8 = 88
11 X 9 = 99
11 X 10 = 110``````
``````#  Python 3 Program
#  Generate multiplication table of given number
class MyMath :
# Display tables of a given number
def table_of_number(self, number) :
if (number <= 0) :
return

result = 0
i = 1
print("\nNumber ", number ,"\n", end = "")
# display table result from 1 to 10
while (i <= 10) :
# Multiply the given number by i value
result = (number * i)
print("", number ," X ", i ," = ", result ,"\n", end = "")
i += 1

def main() :
obj = MyMath()
# Simple Case
obj.table_of_number(7)
obj.table_of_number(5)
obj.table_of_number(11)

if __name__ == "__main__": main()``````

#### Output

``````Number  7
7  X  1  =  7
7  X  2  =  14
7  X  3  =  21
7  X  4  =  28
7  X  5  =  35
7  X  6  =  42
7  X  7  =  49
7  X  8  =  56
7  X  9  =  63
7  X  10  =  70

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

Number  11
11  X  1  =  11
11  X  2  =  22
11  X  3  =  33
11  X  4  =  44
11  X  5  =  55
11  X  6  =  66
11  X  7  =  77
11  X  8  =  88
11  X  9  =  99
11  X  10  =  110``````
``````#  Ruby Program
#  Generate multiplication table of given number
class MyMath

# Display tables of a given number
def table_of_number(number)

if (number <= 0)

return
end
result = 0
i = 1
print("\nNumber ", number ,"\n")
# display table result from 1 to 10
while (i <= 10)

# Multiply the given number by i value
result = (number * i)
print("", number ," X ", i ," = ", result ,"\n")
i += 1
end
end
end
def main()

obj = MyMath.new()
# Simple Case
obj.table_of_number(7)
obj.table_of_number(5)
obj.table_of_number(11)
end
main()``````

#### Output

``````Number 7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70

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

Number 11
11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
11 X 4 = 44
11 X 5 = 55
11 X 6 = 66
11 X 7 = 77
11 X 8 = 88
11 X 9 = 99
11 X 10 = 110
``````
``````// Scala Program
// Generate multiplication table of given number
class MyMath
{
//Display tables of a given number
def table_of_number(number: Int): Unit = {
if (number <= 0)
{
return;
}
var result: Int = 0;
var i: Int = 1;
print("\nNumber " + number + "\n");
//display table result from 1 to 10
while (i <= 10)
{
//Multiply the given number by i value
result = (number * i);
print("" + number + " X " + i + " = " + result + "\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMath = new MyMath();
//Simple Case
obj.table_of_number(7);
obj.table_of_number(5);
obj.table_of_number(11);
}
}``````

#### Output

``````Number 7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70

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

Number 11
11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
11 X 4 = 44
11 X 5 = 55
11 X 6 = 66
11 X 7 = 77
11 X 8 = 88
11 X 9 = 99
11 X 10 = 110``````
``````// Swift Program
// Generate multiplication table of given number
class MyMath
{
//Display tables of a given number
func table_of_number(_ number: Int)
{
if (number <= 0)
{
return;
}
var result: Int = 0;
var i: Int = 1;
print("\nNumber ", number ,"\n", terminator: "");
//display table result from 1 to 10
while (i <= 10)
{
//Multiply the given number by i value
result = (number * i);
print(number ," X ", i ," = ", result ,"\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyMath = MyMath();
//Simple Case
obj.table_of_number(7);
obj.table_of_number(5);
obj.table_of_number(11);
}
main();``````

#### Output

``````Number  7
7  X  1  =  7
7  X  2  =  14
7  X  3  =  21
7  X  4  =  28
7  X  5  =  35
7  X  6  =  42
7  X  7  =  49
7  X  8  =  56
7  X  9  =  63
7  X  10  =  70

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

Number  11
11  X  1  =  11
11  X  2  =  22
11  X  3  =  33
11  X  4  =  44
11  X  5  =  55
11  X  6  =  66
11  X  7  =  77
11  X  8  =  88
11  X  9  =  99
11  X  10  =  110``````

## Time Complexity

The time complexity of this code is constant, O(1), as the calculations performed inside the `table_of_number` function involve basic arithmetic operations. The execution time remains constant regardless of the input value.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post