How to take 2d Array Input in C? In this article, we will see how to take input from users in C language.
Table of Contents
2d Array Input
Taking input for a 2d array in C involves utilizing a nested loop to iterate two rows and columns, prompting the user to input the values at each position. The process is straightforward and requires an understanding of how to use scanf
for input and how to properly iterate over the array dimensions,
Array Input in C Example
#include <stdio.h>
void main()
{
int row, column;
printf("Enter the number of rows ");
scanf("%d", &row);
printf("Enter the number of column ");
scanf("%d", &column);
int array[row][column];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
printf("Enter the element at position {%d,%d}", i, j);
scanf("%d", &array[i][j]);
}
}
printf("Matrix (%d*%d) entered by user -\n", row, column);
for (int k = 0; k < row; k++)
{
for (int l = 0; l < column; l++)
{
printf("%d\t", array[k][l]);
}
printf("\n");
}
}
Output
The above C program first asks users to enter several rows and columns. After that, it asks users to enter elements of the 2d array(as a sizer specified by the user). At the end, the program prints the 2d array. After taking 2d array input we can perform all required operations on 2d array.
Happy Coding & Learning
1 thought on “2d Array Input in C”