Create a Multiplication Table in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn to write a C program to print the multiplication table of any given number. The program will take the user input number and print the table up to 10 multiples of that number.

Creating the Main Function

#include <stdio.h>

int main()
{
    int n, i;

    printf("Enter an integer you need to print the table of: ");
    scanf("%d", &n);

    printf("\nMultiplication table of %d:\n", n); // Printing the title of the table

    // Multiplication logic
    for (i = 1; i <= 10; i++)
        printf("%d x %d = %d\n", n, i, n * i);

    return 0;
}

In the above code, we have created the main function which takes user input integer n and prints the multiplication table of the given number.

Taking User Input

int n;

printf("Enter an integer you need to print the table of: ");
scanf("%d", &n);

In the above code, we are taking user input of an integer value and storing it in a variable named n. We are using scanf function to read the input value.

Printing the Title of the Multiplication Table

printf("\nMultiplication table of %d:\n", n);

We are using the above code to print the title of the multiplication table. We have used \n to add a line break for better readability.

Multiplication Logic

for (i = 1; i <= 10; i++)
    printf("%d x %d = %d\n", n, i, n * i);

In this step, we have used a for loop to print the multiplication table up to ten multiples of the given number. We multiply the number n with the counter variable i and print the result using printf function.

Final program code

Copy and paste the final program code into the main.c file located in the ~/project/ directory:

#include <stdio.h>

int main()
{
    int n, i;

    printf("Enter an integer you need to print the table of: ");
    scanf("%d", &n);

    printf("\nMultiplication table of %d:\n", n); // Printing the title of the table

    // Multiplication logic
    for (i = 1; i <= 10; i++)
        printf("%d x %d = %d\n", n, i, n * i);

    return 0;
}

Summary

In this lab, you have learned to print the multiplication table of any given number. We have created a program that takes user input, prints the title of the table, and then uses multiplication logic to display ten multiples of the input number. By following this step by step guide, you can now create your own multiplication table program in C.

Other C Tutorials you may like