Write a c program to the print the table of any number using for loop
Table of any number using for loop
To write a C program to print the table of any number using a for loop, we can use the following steps:
- Declare two variables: num to store the number whose table we want to print, and i to iterate over the loop.
- Prompt the user to enter the number whose table they want to print.
- Use a for loop to iterate from i = 1 to i = 10.
- In each iteration of the for loop, print the product of num and i to the console.
Here is a C program to print the table of any number using a for loop:
C programming
#include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
printf("The table of %d is:\n", num);
for (i = 1; i <= 10; i++) {
printf("%d * %d = %d\n", num, i, num * i);
}
return 0;
}
Output:
Enter a number: 5 The table of 5 is: 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
We can also modify the program to print the table of a number up to any given limit. For example, the following program will print the table of 5 up to the limit 20:
C programming
#include <stdio.h>
int main() {
int num, i, limit;
printf("Enter a number: ");
scanf("%d", &num);
printf("Enter the limit: ");
scanf("%d", &limit);
printf("The table of %d up to %d is:\n", num, limit);
for (i = 1; i <= limit; i++) {
printf("%d * %d = %d\n", num, i, num * i);
}
return 0;
}
Output:
Enter a number: 5 Enter the limit: 20 The table of 5 up to 20 is: 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 5 * 11 = 55 5 * 12 = 60 5 * 13 = 65 5 * 14 = 70 5 * 15 = 75 5 * 16 = 80 5 * 17 = 85 5 * 18 = 90 5 * 19 = 95 5 * 20 = 100
For loops are a powerful tool for writing C programs. We can use them to perform repetitive tasks in a concise and efficient way.
Top Resources
Multiplication table of a given number
Further Reading:
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!