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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123287{{"`Create a Multiplication Table in C`"}} c/comments -.-> lab-123287{{"`Create a Multiplication Table in C`"}} c/variables -.-> lab-123287{{"`Create a Multiplication Table in C`"}} c/data_types -.-> lab-123287{{"`Create a Multiplication Table in C`"}} c/operators -.-> lab-123287{{"`Create a Multiplication Table in C`"}} c/for_loop -.-> lab-123287{{"`Create a Multiplication Table in C`"}} c/user_input -.-> lab-123287{{"`Create a Multiplication Table in C`"}} c/memory_address -.-> lab-123287{{"`Create a Multiplication Table in C`"}} c/pointers -.-> lab-123287{{"`Create a Multiplication Table in C`"}} c/function_declaration -.-> lab-123287{{"`Create a Multiplication Table in C`"}} end

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