C Program to Print Multiplication Table Using While Loop and For Loop
In the programming world, loops are the most basic. It is only by mastering the rules of a loop, and then replacing them with a loop when necessary, that we can program something that is reproducible. This article explores how to print a multiplication table in C using two types of loops: the `for` loop and the `while` loop. In fact, both approaches are often used—not only in commercial programming languages such as Basic but also in later ones like C. Both are also very simple and straightforward, helping you to consolidate your programming skills.
What is a Multiplication Table?
- Its is very easy to print multiplication table using c program
- We need one for loop which iterates from 1 to 10
- Inside for loop just multiply numbers and print result in each iteration.
- Lets us see an example C program on how to print multiplication table using for loop.
- multiplication table program in c using for loop
- write a c program to input a number from user and print multiplication table of the given number using for loop. how to print multiplication table of a given number in c programming.
- #include <stdio.h>
- int main()
- {
- int n, i;
- printf("Enter a Number ");
- scanf("%d",&n);
- for(i=1; i<=10; ++i)
- {
- printf("%d * %d = %d \n", n, i, n*i);
- }
- getch();
- }
Output:
Program #2: Write a c program to print multiplication table using while loop
write a c program to print multiplication table of any number using for loop
Output:
You Might Like:
- #include <stdio.h>
- int main()
- {
- int n, i;
- printf("Enter a Number ");
- scanf("%d",&n);
- i=1;
- while(i<=10){
- printf("%d * %d = %d \n", n, i, n*i);
- ++i;
- }
- getch();
- }
Output:
- Enter a Number
- 2
- 2 * 1 = 2
- 2 * 2 = 4
- 2 * 3 = 6
- 2 * 4 = 8
- 2 * 5 = 10
- 2 * 6 = 12
- 2 * 7 = 14
- 2 * 8 = 16
- 2 * 9 = 18
- 2 * 10 = 20
Using a `while` Loop to Print a Multiplication Table
The `while` loop is a control flow statement that allows code to be executed repeatedly based on a given condition. If you want to print the multiplication table of a given number using a `while` loop, here is how you can do it in C:
- #include <stdio.h>
- int main() {
- int number, i = 1;
- // Input the number for which you want to print the multiplication table
- printf("Enter a number: ");
- scanf("%d", &number);
- // Use a while loop to print the multiplication table
- printf("Multiplication Table of %d using while loop:\n", number);
- while (i <= 10) {
- printf("%d x %d = %d\n", number, i, number * i);
- i++;
- }
- return 0;
- }
Explanation:
- Input the Number: The program first asks the user to input a number for which the multiplication table will be generated.
- Initialize `i`: The variable `i` is initialized to 1, which will act as the multiplicand.
- While Loop Condition: As long as `i` is less than or equal to 10, the loop continues.
- Print the Result: Within the loop, the program prints the result of `number * i`.
- increment `i`: After each iteration, `i` is incremented by 1 to prepare for the next run.
Output Example:
- Enter a number: 7
- Multiplication Table of 7 using while loop:
- 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
Using a `for` Loop to Print a Multiplication Table
The `for` loop is another control flow statement providing a compact way to write loops. If you want to print the multiplication table of a number using a `for` loop, here is how you can do it in C:
- #include <stdio.h>
- int main() {
- int number;
- // Input the number for which you want to print the multiplication table
- printf("Enter a number: ");
- scanf("%d", &number);
- // Use a for loop to print the multiplication table
- printf("Multiplication Table of %d using for loop:\n", number);
- for (int i = 1; i <= 10; i++) {
- printf("%d x %d = %d\n", number, i, number * i);
- }
- return 0;
- }
Explanation:
1. input the Number: Just as in the example with the `while` loop, this program first asks the user to input a number.
2. For Loop Initialization: The `for` loop initializes `i` to 1, checks the condition `i <= 10`, and increments `i` on each iteration.
3. Print the Result: The program prints the result of `number * i` inside the loop.
Output:
- Enter a number: 7
- Multiplication Table of 7 using for loop:
- 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
Key Differences Between a `while` Loop and a `for` Loop
Syntax: The syntax of the `for` loop is more succinct than that of the `while` loop. With the `for` loop, we can combine initialization, condition testing, and incrementation all into one line; whereas, in the `while` loop, these steps must be executed separately.
When to Use: Use a `while` loop when you don't know how many times the statement execution will be repeated. Use a `for` loop when you know the number of iterations in advance.
Both loops are powerful tools in the C language for handling repetitive work. By understanding how to use them to print a multiplication table, you can apply these methods to solve more complex problems in the future. Whether you choose a `while` loop or a `for` loop totally depends on your actual use case and coding style. I hope you have fun playing around with the code to understand the range of the multiplication table generated, or even combining the two loops under one program to enhance your understanding of them all. Happy programming!
- Write a c program to generate fibonacci series without recursion
- Write a c program to generate fibonacci series using recursion
- Matrix multiplication in c program with explanation
- Factorial program in c without recursion
- C program to check number is armstrong number or not
- Check Armstrong number in c using recursive function
- C program for even or odd using for loop
- C program to check odd or even without using modulus operator and division operator
- Prime number program in c using for loop and while loop
- C program to print prime numbers from 1 to n
- C program to print multiplication table using while loop and for loop
- C program to convert binary to decimal using for loop
- C program to reverse a number using while loop and for loop
- C program to find sum of n numbers using for loop
- C program to find leap year using if else
- c program to check leap year using conditional operator
- C program to print patterns of numbers,alphabets and stars in a pyramid shape
- C program to swap two numbers without using third variable and using functions
- C Program to swap two numbers without using third variable
- Write a C program to swap two integer arrays
- C program for swapping of two strings
- C program to print given number in words
- C program to print 1 to 100 without using loop
- C program to find ASCII value of a string
- Convert string to integer c programming
- C program to display characters from a to z using loop
- C programming power function example program
- C program for addition subtraction multiplication and division using switch case
- C program to delete an element in an array
- C program to insert an element in an array
- Switch case in c example program
- Prime number program in c using for loop
- Fibonacci series in c without recursion
Program to find even or odd with PDF file
ReplyDeleteAsmm program
ReplyDeleteI want program using do while loop
ReplyDelete