C

C Program For Creating A Multiplication Table

This tutorial will teach you how to write a C program that generates a multiplication table. This article will look at two programs. The first program prints the multiplication table for the number entered. In the second example, the multiplication table is displayed up to the specified range.

Example 1: Create a multiplication table using a program

In this example, we’re going to use a for loop to print out the multiplication table. The user is asked to enter an integer, and the program then runs a loop from i= 1 to 10, with the printf() function printing number * I for each iteration of the loop.

#include <stdio.h>
int main() {
  int number, i;
  printf("Enter an integer: ");
  scanf("%d", &number);
  printf("Multiplication table of %d: \n", number);
  for (i = 1; i <= 10; ++i) {
    printf("%d * %d = %d \n", number, i, number * i);
  }
  return 0;
}

Example 2: A program that displays a multiplication table until a certain range is reached

We are not displaying the entire multiplication table in this example. We are taking two user inputs here: the number and the range. The range denotes the step at which the table must be printed.
In this example, we also use a do-while loop to determine whether the range entered by the user is positive. If the user enters a negative range, this loop prompts them to enter a positive range. This loop will continue to run and prompt the user for the correct input until the user enters the positive range.

#include <stdio.h>
int main() {
  int number, i, range;
  printf("Enter an integer: ");
  scanf("%d", &number);

  // If user doesn't enter positive range value
  // repeat the prompt.
  do{
    printf("Enter the range: ");
    scanf("%d", &range);
  } while (range <= 0);
  printf("Multiplication table of %d till range %d: \n", number, range);
  for (i = 1; i <= range; ++i) {
    printf("%d * %d = %d \n", number, i, number * i);
  }
  return 0;
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button