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

For Loop In C Programming Language

Today lets learn about another loop control statement i.e., for loop. For loop is used to repeatedly execute certain set of instructions.

For loop allows us to specify 3 things in a single line:
1. Loop count initialization.
2. Condition Checking.
3. Modification Statement(increment/decrement statements).

Related Read
while loop in C programming
Relational Operators In C
Logical Operators In C
Video Tutorial: For Loop In C Programming Language


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

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

Source Code: For Loop In C Programming Language

#include<stdio.h>
int main()
{
    int i;

    for(i = 0; i < 10; i++)
    {
        printf("%d IBM\n", i );
    }

    return 0;
}

Output:
0 IBM
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM

#include<stdio.h>
int main()
{
    int i;

    for(i = 0; i < 10; i++)
    {
        printf("%d IBM\n", i + 1);
    }

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

i = 1 and i<= 10

#include<stdio.h>

int main()
{
    int i;

    for(i = 1; i <= 10; i++)
    {
        printf("%d IBM\n", i);
    }

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

No initialization statement and No Modification Statement

#include<stdio.h>
int main()
{
    int i = 0;

    for(; i < 10;)
    {
        printf("%d IBM\n", i + 1);
        i = i + 1;
    }

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

No Curly braces and No initialization statement

#include<stdio.h>

int main()
{
    int i = 0;

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

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

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

while loop in C programming

So far we’ve seen sequential flow of our programs. Next we saw decision control statements like if, if else, else if etc. Now lets learn about loop control statements present in C programming language.

In this video tutorial lets learn about while loop in particular.

Related Read:
Programming Interview / Viva Q&A: 5 (Infinite or Indefinite while loop)

General Syntax of while loop

 
#include < stdio.h >

int main()
{
    while(pre_test_condition)
      statement1;

    return 0;
}

Here while is the keyword. Inside parenthesis we need to write the condition. These conditions must evaluate to Boolean value. i.e., true(non-zero number) or false(0);

The ‘body of while’ loop keeps executing until the condition is true. Control exits while loop once the condition is false.

 
#include < stdio.h >

int main()
{
    while(pre_test_condition)
   {
      statement1;
      statement2;
   }
    return 0;
}

Enclose the statements with curly braces if there are multiple statements within while loop or body of while loop.

while loop in C programming Language


[youtube https://www.youtube.com/watch?v=2Mkc80-uqrs]

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


Example Source Code: while loop

 
#include < stdio.h >

int main()
{
    int count = 1;

    while(count <= 5)
    {
        printf("%d\n", count);
        count = count + 1;
    }

    printf("End of loop\n");

    return 0;
}

Output:
1
2
3
4
5
End of loop

In above C source code, variable count is initialized to 1. Inside while condition we check if variable count is less than or equal to 5. Until variable count is less than or equal to 5, the while loop keeps executing. Inside body of the while loop we increment the value of count by 1 upon each execution of the loop. The condition is checked on execution of the loop. Once variable count is more than 5, the condition becomes false count <= 5 (once count value is 6, the condition becomes false), the execution control exits while loop.

Note: Here the loop execution counter is called ‘loop counter’ or ‘index variable’.

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