multiplication table in c using array
Multiplication Table in C Using Array
Here is an example of how to create a multiplication table in C using an array:
#include <stdio.h>
#define SIZE 10
int main() {
int table[SIZE][SIZE];
// Fill the array with the multiplication values
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
table[i][j] = (i + 1) * (j + 1);
}
}
// Print the multiplication table
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%4d ", table[i][j]);
}
printf("\n");
}
return 0;
}
Explanation of each step:
The
SIZE
constant is defined to represent the size of the multiplication table. In this example, it is set to 10.An array
table
of sizeSIZE
bySIZE
is declared to store the multiplication values.Two nested
for
loops are used to fill the array with the multiplication values. The outer loop iterates over the rows, and the inner loop iterates over the columns. The multiplication value for each cell is calculated as(i + 1) * (j + 1)
.Another set of nested
for
loops is used to print the multiplication table. The outer loop iterates over the rows, and the inner loop iterates over the columns. Theprintf
function is used to print each value in a formatted manner.The program ends with a
return 0;
statement, indicating successful execution.
This program will generate a multiplication table from 1 to 10. You can modify the SIZE
constant to generate a larger or smaller table.
I hope this helps! Let me know if you have any further questions.