For instance, let's consider creating the multiplication table of 5 in the C programming language. Initially, we prompt the user to input the number 5. Following that, we implement a loop or a function to calculate the product of 5 with each number from 1 to 10, resulting in the complete multiplication table for the specified number.
Different ways to generate the table program
Following are the various ways to generate the table program in the C programming language.
- Using for loop
- Using while loop
- Using do-while loop
- Using user-defined function
- Using recursion function
- Using if and goto statement
- Using for loop and pointer
- Using nested for loop
Program to generate the table of a given number using for loop
Let's explore an illustration demonstrating how to display the multiplication table of a particular number by utilizing a for loop in the C programming language.
Program1.c
#include <stdio.h>
int main()
{
int num, i; // declare a variable
printf (" Enter a number to generate the table in C: ");
scanf (" %d", &num); // take a positive number from the user
printf ("\n Table of %d", num);
// use for loop to iterate the number from 1 to 10
for ( i = 1; i <= 10; i++)
{
printf ("\n %d * %d = %d", num, i, (num*i));
}
return 0;
}
Output
Enter a number to generate the table in C: 7
Table of 7
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
In the provided code snippet, a for loop runs incrementally from 1 to 10. Each iteration involves multiplying the specified number by a sequence of multipliers starting from 1 and increasing by 1 in each subsequent iteration until it reaches 10. The result of each multiplication operation is displayed as a part of the table output on the console.
Program to generate the table of a number using while loop
Let's examine a scenario where we generate a table for a given number by utilizing a while loop in the C programming language.
Program2.c
#include <stdio.h>
int main()
{
int num, i = 1; // declare a variable
printf (" Enter a number to generate the table in C: ");
scanf (" %d", &num); // take a positive number from the user
printf ("\n Table of %d \n ", num);
// use while loop to evaluate the condition
while (i <= 10)
{
// print the table
printf (" %d x %d = %d \n", num, i, (num * i));
i++; // incremented by 1
}
return 0;
}
Output
Enter a number to generate the table in C: 8
Table of 8
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
Program to generate the table of a number using do-while loop
Let's explore an illustration demonstrating how to display the multiplication table using a do-while loop in the C programming language.
Program3.c
#include <stdio.h>
int main()
{
int num, i = 1; // declare a variable
printf (" Enter a number to generate the table in C: ");
scanf (" %d", &num); // take a positive number from the user
printf ("\n Table of %d \n", num);
// use do-while loop to evaluate the condition
do
{
// print the table
printf (" %d x %d = %d \n", num, i, (num * i));
i++; // incremented by 1
} while (i <= 10); // check condition
return 0;
}
Output
Enter a number to generate the table in C: 3
Table of 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Program to generate the table using for loop and user-defined function
Let's examine a scenario where we generate the multiplication table of a given number by utilizing a custom function and a for loop in the C programming language.
Program4.c
#include <stdio.h>
void tab_num (int x);
int main ()
{
int number;
printf (" Enter a number to get the table: ");
scanf (" %d", &number); // accept a number
printf ("\n The multiplication table of %d \n", number);
tab_num (number); // call tab_num function
return 0;
}
void tab_num (int number)
{
int i; // declare variable
// use for loop to iterate the number from 1 to 10
for ( i = 1; i <= 10; i++)
{
printf (" %d x %d = %d \n", number, i, number * i);
}
}
Output
Enter a number to get the table: 23
The multiplication table of 23
23 x 1 = 23
23 x 2 = 46
23 x 3 = 69
23 x 4 = 92
23 x 5 = 115
23 x 6 = 138
23 x 7 = 161
23 x 8 = 184
23 x 9 = 207
23 x 10 = 230
Program to generate the table of a number using recursion function
Here is an example demonstrating how to generate a number table using a recursive function in the C programming language:
#include <stdio.h>
void printTable(int num, int count) {
if (count > 10)
return;
printf("%d x %d = %d\n", num, count, num * count);
printTable(num, count + 1);
}
int main() {
int number = 5;
printTable(number, 1);
return 0;
}
In this code snippet, the function printTable recursively calculates and prints the multiplication table for a given number up to 10. The main function initializes the process by calling printTable with the number 5 to print the table for 5.
Program5.c
#include <stdio.h>
int main()
{
int num, i = 0; // declare a variable
printf (" Enter a number to generate the table in C: ");
scanf (" %d", &num); // take a positive number from the user
printf ("\n Table of %d", num);
Multiply_tab (num, 1); // call function
}
// definition of Multiply_tab()
void Multiply_tab (int num, int i)
{
printf (" \n");
printf (" %d x %d = %d ", num, i, (num * i));
// If block checks the condition
if (i < 10)
return Multiply_tab (num, i + 1);
}
Output
Enter a number to generate the table in C: 9
Table of 9
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
Program to generate the table of a number using goto statement
Let's explore an illustration of displaying the multiplication table using a goto statement in the C programming language.
Program6.c
#include <stdio.h>
int main ()
{
int n, i = 1; // declare variable
printf (" Enter a number to get the table: ");
scanf (" %d", &n); // take a positive number
table: // It is a label
// display table
printf (" %d * %d = %d \n", n, i, (n * i));
i++; // incremented by 1
/* if statement check the value of i should not be greater than 10. */
if ( i <= 10)
goto table; // use goto statement to jump to the table label
}
Output
Enter a number to get the table: 5
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 generate the table of a number using pointer
Let's explore an illustration demonstrating the process of displaying the multiplication table using a pointer in the C programming language.
Program7.c
#include <stdio.h>
int main ()
{
int n, i; // declare integer variables
int *ptr; // integer pointer variable
printf (" Enter a number to print the table: ");
scanf (" %d", &n); // take a number
ptr = &n; // store the address of n to ptr
printf (" \n Table of %d: \n", n);
for ( i = 1; i <= 10; i++)
{
printf (" %d * %d = %d \n", n, i, (n*i));
}
return 0;
}
Output
Enter a number to print the table: 17
Table of 17:
17 * 1 = 17
17 * 2 = 34
17 * 3 = 51
17 * 4 = 68
17 * 5 = 85
17 * 6 = 102
17 * 7 = 119
17 * 8 = 136
17 * 9 = 153
17 * 10 = 170
Program to generate the table from 2 to 10 using nested for loop
Let's explore an illustration demonstrating how to display a table ranging from 2 to 10 by utilizing nested for loops in the C programming language.
Program8.c
#include <stdio.h>
int main ()
{
int i, j; /* Here, the i variable is used for the outer loop, and j is used for the inner loop. */
int num1, num2;
printf (" Enter the first number from to get the table: ");
scanf (" %d", &num1);
printf (" Enter the last number: ");
scanf (" %d", &num2);
// use for loop to iterate the value of 1 up to 10
for (i = num1; i <= num2; i++)
{
num1 = i; /* Here the value of i is incremented by 1 and assigned to num variable on completion of each table. */
// inner for loop
printf (" Table of %d => ", num1);
for (j = 1; j <=10; j++)
{
printf(" | %3d |", num1 * j);
}
printf (" \n");
}
return 0;
}
Output
Enter the first number from to get the table: 2
Enter the last number: 10
Table of 2 => | 2 | | 4 | | 6 | | 8 | | 10 | | 12 | | 14 | | 16 | | 18 | | 20 |
Table of 3 => | 3 | | 6 | | 9 | | 12 | | 15 | | 18 | | 21 | | 24 | | 27 | | 30 |
Table of 4 => | 4 | | 8 | | 12 | | 16 | | 20 | | 24 | | 28 | | 32 | | 36 | | 40 |
Table of 5 => | 5 | | 10 | | 15 | | 20 | | 25 | | 30 | | 35 | | 40 | | 45 | | 50 |
Table of 6 => | 6 | | 12 | | 18 | | 24 | | 30 | | 36 | | 42 | | 48 | | 54 | | 60 |
Table of 7 => | 7 | | 14 | | 21 | | 28 | | 35 | | 42 | | 49 | | 56 | | 63 | | 70 |
Table of 8 => | 8 | | 16 | | 24 | | 32 | | 40 | | 48 | | 56 | | 64 | | 72 | | 80 |
Table of 9 => | 9 | | 18 | | 27 | | 36 | | 45 | | 54 | | 63 | | 72 | | 81 | | 90 |
Table of 10 => | 10 | | 20 | | 30 | | 40 | | 50 | | 60 | | 70 | | 80 | | 90 | | 100 |