Table In Function


Explanation

A function can receive a value as a parameter and use it to perform a specific task. In this program, the user-defined function table() accepts a number and prints its multiplication table using a for loop. The same function is called multiple times with different numbers to display different tables.

How It Works?

  • Step 1: Include the required header files stdio.h and conio.h.
  • Step 2: Create a user-defined function table(int no) that accepts a number as a parameter.
  • Step 3: Declare the variable k inside the function.
  • Step 4: Use a for loop to print the multiplication table from 0 to 10.
  • Step 5: In the main() function, call table(4) and table(6).
  • Step 6: The function prints the multiplication tables of both numbers.
  • Step 7: Stop the screen using getch().

Example Program


#include <stdio.h>
#include <conio.h>

void table(int no)
{

    int k;

    for(k = 0; k <= 10; k++)

    {

        printf("\n %d x %d = %d", no, k, k * no);

    }

}

int main()
{

    clrscr();

    table(4);

    table(6);

    getch();

}

Output

4 x 0 = 0
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40

6 x 0 = 0
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

Practice Exercises

Task 1: Write a C program using a function named table() to print the multiplication table of a given number from 1 to 10. Call the function for the numbers 5 and 8.

Goal: Learn how to pass arguments to a user-defined function and use a for loop to print multiplication tables.

💡 Hint: Create a function table(int no). Use a for loop from 1 to 10 and call the function twice from the main() function.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

void table(int no)
{

    int k;

    for(k = 1; k <= 10; k++)
    {

        printf("\n %d x %d = %d", no, k, no * k);

    }

}

int main()
{

    clrscr();

    table(5);

    table(8);

    getch();

}

Output
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

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
Explanation: The program defines a user-defined function table() that accepts a number as a parameter. Inside the function, a for loop prints the multiplication table from 1 to 10. The function is called twice from the main() function to print the tables of 5 and 8, demonstrating how one function can be reused with different arguments.