Multiplication tables are useful for practicing nested loops. The outer loop selects a row and the inner loop generates the values across that row.
#include <stdio.h>
int main(void) {
int row = 1;
while (row <= 10) {
int column = 1;
while (column <= 10) {
printf("%4d", row * column);
column++;
}
putchar('\n');
row++;
}
return 0;
}
#include <stdio.h>
int main(void) {
int number;
printf("Enter a number: ");
if (scanf("%d", &number) != 1) {
printf("Invalid input.\n");
return 1;
}
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n",
number, i, number * i);
}
return 0;
}
#include <stdio.h>
int main(void) {
for (int row = 10; row >= 1; row--) {
for (int column = 10; column >= 1; column--) {
printf("%4d", row * column);
}
putchar('\n');
}
return 0;
}
#include <stdio.h>
int main(void) {
for (int row = 1; row <= 10; row++) {
int row_sum = 0;
for (int column = 1; column <= 10; column++) {
int product = row * column;
printf("%4d", product);
row_sum += product;
}
printf(" | Sum = %d\n", row_sum);
}
return 0;
}
Related: while loops, for loops, and loop patterns.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.