Table Program in C
0 285
Program for Tables in C Using do-while loop
This program uses a do-while loop to generate the multiplication table.
The do-while loop will always execute the code block at least once before checking the loop condition.
In this program, we initialize i to 1 and then print the multiplication table for the given number until i reaches 10.
// Program using do while loop #include<stdio.h>int main() { int num, i = 1; printf("Enter a number to generate the table in C: "); scanf("%d", &num); printf("\nTable of %d\n", num); do { printf("%d x %d = %d\n", num, i, (num * i)); i++; } while (i <= 10); return 0; }
Output:
Enter a number to generate the table in C: 4 Table of 4 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 4 x 10 = 40
Program for tables in C Using user-defined function with for loop
This program uses a user-defined function to generate the multiplication table for a given number.
The printTable function takes an integer parameter and prints the multiplication table for that number using a for loop.
// Program using user defined function #include<stdio.h>void printTable(int num) { for (int i = 1; i <= 10; i++) { printf("%d x %d = %d\n", num, i, (num * i)); } } int main() { int num; printf("Enter a number to generate the table: "); scanf("%d", &num); printf("\nTable of %d\n", num); printTable(num); return 0; }
Output:
Enter a number to generate the table: 3 Table of 3 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30
Share:
Comments
Waiting for your comments