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

Leave a Reply

Your email address will not be published. Required fields are marked *