Exercise:
Write a C Program to generate multiplication table.
1. Print table up to 10
Click Here to View the Solution!
#include <stdio.h>
int main() {
int num, i;
printf("Enter an integer: ");
scanf("%d", &num);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", num, i, num * i);
}
return 0;
}
Click Here to View the Output!
Enter an integer: 6
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
Click Here to View the Explanation!
- This program is used to generate and display a multiplication table of a number up to 10.
- In the
main method
, an integer variablei
is initialized. - The program requests the user to enter an integer and stores it in an integer variable n using the
scanf()
function. - In a
for loop
, the iteration continues forn
number of times and each time n is multiplied withi
. The value ofi
increments by one in each iteration. - The table is formed by specifying the format in the
printf()
function as follows (%d * %d = %d, n, i, n * i). - The
return 0 statement
is used to exit the program execution.
2. Print table up to A specific range
Click Here to View the Solution!
#include <stdio.h>
int main() {
int num, i, range;
printf("Enter an integer: ");
scanf("%d", &num);
printf("Enter the range: ");
scanf("%d", &range);
for (i = 1; i <= range; ++i) {
printf("%d * %d = %d \n", num, i, num * i);
}
return 0;
}
Click Here to View the Output!
Enter an integer: 6
Enter the range: 4
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
Click Here to View the Explanation!
- This program is used to generate and display a multiplication table of a number up to a specified range.
- In the
main method
, an integer variablei
is initialized. - The program requests the user to enter an integer and a range and stores them in the
integer
variables n and range using thescanf()
function. - In a
for loop
, the iteration continues for range number of times and each time n is multiplied withi
. The value ofi
increments by one in each iteration. - The table is formed by specifying the format in the
printf()
function as follows (%d * %d = %d, n, i, n * i). - The
return 0 statement
is used to exit the program execution.