C Program To Print Multiplication Table Using Macros

In this video lets see how we can print multiplication table(from 1 to 10) for any positive user input value, using Macros.

Related Read:
C Program To Print Multiplication Table Using While Loop

Video Tutorial: C Program To Print Multiplication Table Using Macros


[youtube https://www.youtube.com/watch?v=1IaEnPHRalw]

YouTube Link: https://www.youtube.com/watch?v=1IaEnPHRalw [Watch the Video In Full Screen.]

Source Code: C Program To Print Multiplication Table Using Macros

#include<stdio.h>

#define MULTI(num, count) printf("%d x %d = %d\n", num, count, (num*count))

int main()
{
    int num, count = 1;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nMultiplication Table for %d is: \n\n", num);

    while(count <= 10)
    {
        MULTI(num, count);
        count++;
    }

    return 0;
}

Output 1:
Enter a positive number
5

Multiplication Table for 5 is:

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

Output 2:
Enter a positive number
4

Multiplication Table for 4 is:

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

Here we are initializing variable count to 1 and iterating the while loop until count is less than or equal to 10. For each iteration we’re executing macro MULTI(num, count). But before compilation itself preprocessor will have replaced MULTI(num, count) by its macro expansion, which is printf(“%d x %d = %d\n”, num, count, (num*count)).

Note: If you see \ inside macro expansion – it is called as Macro continuation(\) operator. It is used to continue the code in next line, in macro expansion.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print Multiplication Table Using Function

Lets write a C program to print the multiplication table for a user input number, using function/method. The table should get displayed in the following format:

Example: Multiplication table of 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

Coding Challenge on Numbers!
Print the multiplication table which gives the following output:

multiplication table for number 37

Hint: Initialize count to 3. Increment the value of count by 3 for each iteration of for loop.

Answer: We’ve posted the source code to above coding challenge at the end of this blog post.

Related Read:
C Program To Print Multiplication Table Using For Loop
C Program To Print Multiplication Table Using While Loop

Video Tutorial: C Program To Print Multiplication Table Using Function


[youtube https://www.youtube.com/watch?v=5fafT5Pc1jw]

YouTube Link: https://www.youtube.com/watch?v=5fafT5Pc1jw [Watch the Video In Full Screen.]


Source Code: C Program To Print Multiplication Table Using Function

#include<stdio.h>

void tables(int);

int main()
{
    int num;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nMultiplication Table for %d is:\n", num);

    tables(num);

    return 0;
}

void tables(int num)
{
    int count;

    for(count = 1; count <= 10; count++)
    {
        printf("%d x %d = %d\n", num, count, num*count);
    }
}

Output 1:
Enter a positive number
5

Multiplication Table for 5 is:
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

Output 2:
Enter a positive number
2

Multiplication Table for 2 is:
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

Output 3:
Enter a positive number
7

Multiplication Table for 7 is:
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

Output 4:
Enter a positive number
9

Multiplication Table for 9 is:
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

Logic: Multiplication Table

User enters a positive number, we store it inside variable num. We pass this number entered by the user to a function called tables().

Inside tables() function we write a for loop. We initialize the for loop counter variable count to 1 and iterate through this for loop until count is less than or equal to 10 – because we want to display multiplication tables from 1 to 10. For each iteration of for loop we increment the value of count by 1.

Inside for loop we display the multiplication table.

You can also watch C Program To Print Multiplication Table Using While Loop video tutorial.

Just For Some Number Fun

Here I’m initializing 3 to count variable. For loop executes until count is less than or equal to 27. For each iteration of for loop count value increments by 3.

We assign 37 to num variable. Check the output.

Source Code: Some numerology!

#include<stdio.h>

void tables(int);

int main()
{
    int num = 37;

    printf("\nMultiplication Table for %d is:\n", num);

    tables(num);

    return 0;
}

void tables(int num)
{
    int count;

    for(count = 3; count <= 27; count += 3)
    {
        printf("%d x %d = %d\n", num, count, num*count);
    }
}

Output:
Multiplication Table for 37 is:

37 x 3 = 111
37 x 6 = 222
37 x 9 = 333
37 x 12 = 444
37 x 15 = 555
37 x 18 = 666
37 x 21 = 777
37 x 24 = 888
37 x 27 = 999

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print Multiplication Table Using For Loop

Lets write a C program to print the multiplication table of the number entered by the user. The table should get displayed in the following form:

Example: Multiplication table of 29
29 x 1 = 29
29 x 2 = 58
29 x 3 = 87
29 x 4 = 116
29 x 5 = 145
29 x 6 = 174
29 x 7 = 203
29 x 8 = 232
29 x 9 = 261
29 x 10 = 290

Related Read:
Basic Arithmetic Operations In C
For Loop In C Programming Language

Simple Mathematical Trick — logical thinking

C Program To Print Multiplication Table Using For Loop


[youtube https://www.youtube.com/watch?v=fjEYZStx5_I]

YouTube Link: https://www.youtube.com/watch?v=fjEYZStx5_I [Watch the Video In Full Screen.]


Source Code: C Program To Print Multiplication Table Using For Loop

 
#include<stdio.h>

int main()
{
    int num, count;

    printf("Enter a number\n");
    scanf("%d", &num);

    printf("Multiplication table for %d is:\n", num);

    for(count = 1; count <= 10; count++)
    {
        printf("%d x %d = %d\n", num, count, (num*count));
    }

    return 0;
}

Output 1:
Enter a number
5
Multiplication table for 5 is:
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

Output 2:
Enter a number
10
Multiplication table for 10 is:
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

Output 3:
Enter a number
7
Multiplication table for 7 is:
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

Output 4:
Enter a number
6
Multiplication table for 6 is:
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

Logic: Multiplication Table

We ask the user to enter a number. We initialize for loop counter to 1 and iterate through the loop until loop counter is less than or equal to 10. Inside for loop we multiply the user entered number with the loop counter variable to get the result of multiplication table.

You can also watch C Program To Print Multiplication Table Using While Loop video tutorial.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print Multiplication Table Using While Loop

Lets write a C program to ask the user to input an integer value and then output multiplication table(up to 10) on to the console window.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming

Simple Mathematical Trick — logical thinking

Source Code: C Program To Print Multiplication Table Using While Loop

 
#include < stdio.h >

int main()
{
    int num, count = 1;

    printf("Enter a number\n");
    scanf("%d", &num);

    printf("\nMultiplication table for %d is:\n\n", num);
    while(count <= 10)
    {
        printf("%d x %d = %d\n", num, count, (num*count));
        count++;
    }

    return 0;
}

Output 1:
Enter a number
2

Multiplication table for 2 is:

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

Output 2:
Enter a number
9

Multiplication table for 9 is:

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

Output 3:
Enter a number
14

Multiplication table for 14 is:

14 x 1 = 14
14 x 2 = 28
14 x 3 = 42
14 x 4 = 56
14 x 5 = 70
14 x 6 = 84
14 x 7 = 98
14 x 8 = 112
14 x 9 = 126
14 x 10 = 140

Output 4:
Enter a number
10

Multiplication table for 10 is:

10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

C Program To Print Multiplication Table Using While Loop


[youtube https://www.youtube.com/watch?v=-OfeGt8QV9s]

YouTube Link: https://www.youtube.com/watch?v=-OfeGt8QV9s [Watch the Video In Full Screen.]


Logic: Multiplication Table

We take a variable count and initialize it to 1 and keep increment the value of count by 1, until its value is 11. Once its value is 11 we stop iterating the while loop. This way we can calculate and out put multiplication table for 10 numbers.

Inside the while loop we multiply the user entered number and the value of count, and then display the result on each iteration.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert