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:
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]
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