You are currently viewing C Program To Print Table Number Using While Loop

C Program To Print Table Number Using While Loop

In this tutorial, you will learn how to write a C program to print multiplication or we can say the table of any number using a while loop conditional statement. We will see various examples, algorithms, logics and program explanations to solve this problem in C programming language.

Required Knowledge

Problem Statement

You have to Write a C program that will take a number ‘n’ as input from the user. After taking the input it should print the multiplication table of ‘n’ using a while loop. The program provides a solution to generate and print the multiplication table of any number. Below is the example for better understanding.

Example

Input:
Enter a number: 5

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

Algorithm

  1. Start the program.
  2. Declare the variables: num to store the input number and i as a counter.
  3. Read the value of num from the user.
  4. Initialize i to 1.
  5. Print the header message: “Multiplication table of num:”
  6. Enter the while loop with the condition i <= 10.
    • Calculate the product of num and i.
    • Print the multiplication expression: “num x i = product”.
    • Increment i by 1.
  7. Exit the while loop.
  8. End the program.

Logic used to solve this problem

  • Declare a variable to hold the input number.
  • Prompt the user to enter a number.
  • Read and store the entered number in the variable.
  • Initialize a num variable to 1.
  • Use a while loop that runs as long as the num is less than or equal to 10 (or any desired range).
  • Inside the loop, calculate the multiplication of the input number with the num.
  • Print the multiplication result.
  • Increment the num by 1 in each iteration.
  • Continue the loop until the num reaches the desired range.
  • Finish the program.

C Program to print table of any number

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

Output

Enter a number: 5
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

Program Explanation

  • The program takes input for the number whose multiplication table is to be printed.
  • It initializes a variable num to 1, which represents the current multiplier.
  • The program enters a while loop with the condition num <= 10 to iterate through multipliers from 1 to 10.
  • Inside the while loop, it calculates the product of the input number and the current multiplier.
  • It prints the equation number * multiplier = product on the screen, where number is the input number, multiplier is the current multiplier, and product is the calculated product.
  • It then increments the value of num by 1 to move on to the next multiplier.
  • The loop continues until num exceeds the value of 10.
  • Once the while loop is complete, the program ends.

.

Conclusion

The C program successfully prints the table of any number entered by the user using a while loop. By iterating through the integers from 1 to 10 and calculating the product of the given number with each integer, it displays the multiplication table on the screen. I hope this tutorial helps you to understand the table program in C.