1.
Program to get the input of 2d array from the user and display it
#include <stdio.h>
int main()
int rows, cols;
// Ask the user for the number of rows and columns
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Declare a 2D array
int array[rows][cols];
// Input elements into the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
printf("Element [%d][%d]: ", i, j);
scanf("%d", &array[i][j]);
// Display the array
printf("The array is:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
printf("%d 2",array[i][j] );
printf("\n");
}
return 0;
}
2. C Program to find the sum of 2 matrices
#include <stdio.h>
int main()
// Initialize two matrices
int matrix1[2][2] = {{1, 2}, {3, 4}};
int matrix2[2][2] = {{5, 6}, {7, 8}};
int sum[2][2]; // Matrix to store the result
// Perform matrix addition
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
sum[i][j] = matrix1[i][j] + matrix2[i][j];
// Display the resultant matrix
printf("Sum of Matrix 1 and Matrix 2:\n\n");
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
printf("%d ", sum[i][j]);
printf("\n");
return 0;
}
3. C program to find tranpose of matrix
#include <stdio.h>
int main()
// Initialize a 3x3 matrix
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int transpose[3][3]; // Array to store the transpose
// Calculate the transpose
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
transpose[j][i] = matrix[i][j];
}
}
// Display the original matrix
printf("Original Matrix:\n");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
printf("%d ", matrix[i][j]);
printf("\n");
}
// Display the transpose
printf("\nTranspose Matrix:\n");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
printf("%d ", transpose[i][j]);
printf("\n");
return 0;
}
4. Matrix multiplication in C
#include <stdio.h>
int main() {
// Initialize two matrices
int matrix1[3][3] =
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int matrix2[3][3] =
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int result[3][3] = {0}; // Resultant matrix initialized to 0
// Perform matrix multiplication
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++)
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
// Display the result
printf("Resultant Matrix:\n");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
printf("%d ", result[i][j]);
printf("\n");
return 0;