Multiplication table in C

Multiplication table in C

This blog explores the use of C programming language to create multiplication tables using different methods, highlighting the importance of understanding these tables for developing strong mathematical skills and problem-solving abilities, while also providing insights into programming logic.

Methods of Creating Multiplication Tables in C

Check out various methods to generate multiplication tables in C:

Multiplication Table in C using For Loop

Go through below code for better understanding of Multiplication table in C using for Loop:

//include standard input/output library
#include <stdio.h>

//main function
int main() {
    //declare variables for number and counter
    int num, i;

    //prompt user to enter a number
    printf("Enter a number: ");

    //read input from user and store in variable num
    scanf("%d", &num);

    //use for loop to iterate from 1 to 10
    for (i = 1; i <= 10; i++) {
        //multiply num with current value of i and print the result
        printf("%d x %d = %d\n", num, i, num * i);
    }

    //return 0 to indicate successful execution
    return 0;
}

Explanation of the code:

This C program generates a multiplication table for a specified number entered by the user. 

1. User Input: The program prompts the user to enter a number using `printf()` and reads the input using `scanf()`, storing it in the variable `num`.

2. For Loop: It utilizes a `for` loop to iterate from 1 to 10, with `i` serving as the loop counter.

3. Multiplication Operation: Within the loop, the program computes the product of `num` and `i`, then prints the result in the format `”num x i = num*i”` using `printf()`.

4. Output: The multiplication table for the specified number is displayed in the console.

5. Return Statement: The `main()` function returns 0, indicating successful execution.

Output:

Enter a 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

Multiplication Table in C using While Loop

Check out the below code of Multiplication table in C using While Loop:

//include standard input/output library
#include <stdio.h>

//define main function
int main() {

    //declare variables for input and counter
    int num, i = 1;

    //prompt user to enter a number
    printf("Enter a number: ");

    //read input from user and store in variable num
    scanf("%d", &num);

    //print multiplication table heading
    printf("Multiplication table of %d:\n", num);

    //while loop to print multiplication table
    while (i <= 10) {

        //calculate and print product of num and i
        printf("%d x %d = %d\n", num, i, num * i);

        //increment counter i by 1
        i++;
    }

    //return 0 to indicate successful execution of program
    return 0;
}

Explanation of the code:

This C program generates a multiplication table for a specified number entered by the user using a while loop.

1. User Input: The program prompts the user to enter a number using `printf()` and reads the inpuxt using `scanf()`, storing it in the variable `num`.

2. Multiplication Table Heading: It prints the heading for the multiplication table indicating the number entered by the user.

3. While Loop: It utilizes a `while` loop with the loop counter `i` initialized to 1 and incremented by 1 in each iteration.

4. Multiplication Operation: Within the loop, the program calculates the product of `num` and `i`, then prints the result in the format `”num x i = num*i”` using `printf()`.

5. Output: The multiplication table for the specified number is displayed in the console.

6. Return Statement: The `main()` function returns 0, indicating successful execution.

Output:

Enter a number: 7
Multiplication table of 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

Multiplication Table in C using Do While Loop

Program of C of generating Multiplication table using Do While Loop code is given below:

//include standard input/output library
#include <stdio.h>

//define main function
int main() {
    //declare variables for input and counter
    int num, i = 1;

    //prompt user to enter a number
    printf("Enter a number: ");

    //read input from user and store in variable num
    scanf("%d", &num);

    //print multiplication table heading
    printf("Multiplication Table of %d:\n", num);

    //start do-while loop
    do {
        //calculate and print product of num and counter
        printf("%d x %d = %d\n", num, i, num * i);

        //increment counter by 1
        i++;
    }
    //loop condition: counter should be less than or equal to 10
    while (i <= 10);

    //return 0 to indicate successful execution
    return 0;
}

Explanation of the code:

This C program generates a multiplication table for a specified number entered by the user using a do-while loop.

1. User Input: The program prompts the user to enter a number, which is read and stored in the variable `num` using `scanf()`.

2. Multiplication Table Heading: It prints the heading indicating the number entered by the user.

3. Do-While Loop: It executes a block of code that calculates and prints the product of `num` and the counter `i`, incrementing `i` each time.

4. Loop Condition: The loop continues as long as the counter `i` is less than or equal to 10.

5. Output: The multiplication table for the specified number is displayed in the console.

6. Return Statement: The `main()` function returns 0, indicating successful execution.

Output:

Enter a number: 19
Multiplication Table of 19:
19 x 1 = 19
19 x 2 = 38
19 x 3 = 57
19 x 4 = 76
19 x 5 = 95
19 x 6 = 114
19 x 7 = 133
19 x 8 = 152
19 x 9 = 171
19 x 10 = 190

Multiplication Table in C using Function

Code for Multiplication table in C using Function is mentioned here:

//include standard input/output library
#include <stdio.h>

//define function for multiplication table
void multiplicationTable(int num) {
    //declare variable for counter
    int i;
    
    //use for loop to iterate through numbers 1 to 10
    for(i = 1; i <= 10; i++) {
        //print the multiplication table for given number
        printf("%d x %d = %d\n", num, i, num * i);
    }
}

//main function
int main() {
    //declare variable for input number
    int num;
    
    //ask user for input
    printf("Enter a number: ");
    //store input in variable
    scanf("%d", &num);
    
    //call multiplicationTable function with input number as argument
    multiplicationTable(num);
    
    return 0;
}

Explanation of the code:

This C program generates a multiplication table for a specified number entered by the user using a function.

1. Function Definition: The `multiplicationTable` function takes an integer parameter `num` and calculates the multiplication table for that number.

2. For Loop: Within the function, a `for` loop iterates from 1 to 10, calculating and printing the product of `num` and the loop counter.

3. User Input: In the `main` function, the user is prompted to enter a number, which is stored in the variable `num` using `scanf()`.

4. Function Call: The `multiplicationTable` function is called with the input number as an argument.

5. Output: The multiplication table for the specified number is displayed in the console.

6. Return Statement: The `main()` function returns 0, indicating successful execution.

Output:

Enter a number: 24
24 x 1 = 24
24 x 2 = 48
24 x 3 = 72
24 x 4 = 96
24 x 5 = 120
24 x 6 = 144
24 x 7 = 168
24 x 8 = 192
24 x 9 = 216
24 x 10 = 240

Multiplication Table in C using Recursion

Learn how to make multiplication tables in C with a while loop. See the code below:

//include necessary header files
#include <stdio.h>

//function to print multiplication table recursively
void multiplicationTable(int num, int count)
{
    //base case
    if(count > 10)
        return;
    
    //print the multiplication table
    printf("%d x %d = %d\n", num, count, num*count);
    
    //recursive call to print the next row
    multiplicationTable(num, count+1);
}

//main function
int main()
{
    //declare variables
    int num;
    
    //get input from user
    printf("Enter a number: ");
    scanf("%d", &num);
    
    //call the function to print multiplication table
    multiplicationTable(num, 1);
    
    return 0;
}

Explanation of the code:

This C program generates a multiplication table for a specified number entered by the user using recursion.

1. Function Definition: The `multiplicationTable` function recursively prints the multiplication table for the given number `num`. It takes two parameters: `num` (the number for which the multiplication table is generated) and `count` (the current row number).

2. Base Case: If `count` exceeds 10, indicating the end of the multiplication table, the function returns without further recursion.

3. Printing Multiplication Table: Within the function, the product of `num` and `count` is printed in the format `”num x count = num*count”`.

4. Recursive Call: The function calls itself with incremented `count` to print the next row of the multiplication table.

5. User Input: In the `main` function, the user is prompted to enter a number, which is stored in the variable `num` using `scanf()`.

6. Function Call: The `multiplicationTable` function is called with the input number and starting count as arguments.

7. Output: The multiplication table for the specified number is displayed in the console.

8. Return Statement: The `main()` function returns 0, indicating successful execution.

Output:

Enter a number: 2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

Try out our Multiplication Calculator for faster results!

Understanding multiplication tables is important for getting better at math. Learning how to make them using C programming can help you think more logically. Trying out different ways to do it can make you better at both math and programming. It’s like a fun puzzle that helps you learn. So, by practicing different methods, you can get better at math and coding at the same time!

We hope our blog on “ Multiplication table in C”  was informative for you and provided useful insight about the Multiplication table in C Language. For more essential courses and blogs about C, C+, and Java keep checking our official Newtum website.

About The Author

Leave a Reply