printf("\nMultiplication Table for %d is: \n\n", num);
while(count <= 10)
{
MULTI(num, count);
count++;
}
return 0;
}
#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.
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.
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);
}
}
#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.
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.
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
printf("%d x %d = %d\n", num, count, (num*count));
}
return 0;
}
#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.
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.