In this
example, you will learn to generate the multiplication table of a number
entered by the user.
To understand this
example, you should have the knowledge of the following C
programming topics:
C Programming Operators
C for Loop
The
program below takes an integer input from the user and generates the
multiplication tables up to 10.
Multiplication Table Up
to 10
 
#include 
int main() {
  int n, i;
  printf("Enter an integer: ");
  scanf("%d", &n);
  for (i = 1; i <= 10; ++i) {
    printf("%d * %d = %d \n", n, i, n * i);
  }
  return 0;
}
Output
Enter an integer: 99 * 1 = 99 * 2 = 189 * 3 = 279 * 4 = 369 * 5 = 459 * 6 = 549 * 7 = 639 * 8 = 729 * 9 = 819 * 10 = 90
Here, the user input is
stored in the int variable n. Then, we use a for loop to print
 the multiplication table up to 10.
 
for (i = 1; i <= 10; ++i) {
 
printf("%d * %d = %d \n", n, i, n * i);
}
The loop runs from i = 1 to i = 10. In each
iteration of the loop, n * i is printed.
Here's a little modification of the above program to
generate the multiplication table up to a range (where range is also a positive integer
entered by the user).
Multiplication Table Up
to a range
 
#include 
int main() {
  int n, i, range;
  printf("Enter an integer: ");
  scanf("%d", &n);
  // prompt user for positive range
  do {
    printf("Enter the range (positive integer): ");
    scanf("%d", &range);
  } while (range <= 0);
  for (i = 1; i <= range; ++i) {
    printf("%d * %d = %d \n", n, i, n * i);
  }
  return 0;
}
 Output
Enter an integer: 12Enter the range (positive integer): -8Enter the range (positive integer): 812 * 1 = 12 12 * 2 = 24 12 * 3 = 36 12 * 4 = 48 12 * 5 = 60 12 * 6 = 72 12 * 7 = 84 12 * 8 = 96 Here, we have used a do...while loop to prompt the user for a
positive range.
// prompt user for positive range
do {
 
printf("Enter the range (positive integer): ");
 
scanf("%d", &range);
} while (range <= 0);
If the value
of range is negative, the loop iterates again to ask the user to
enter a positive number. Once a positive range has been entered, we print the
multiplication table.
Labels : #c ,#code ,#examples ,