๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R25

Programming for Problem Solving - Lab Manual

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

Write a C program that uses functions to perform Addition of Two Matrices

โ† All Lab Programs

Write a C program that uses functions to perform Addition of Two Matrices

Algorithm

start
	step1: 
	step2: 
	step3: 
	step4: 
	step5: 
	step6: 
	step7: 
	step8: 
	step9: 
	step10:
stop

Flowchart
flowchart for Addition of Two Matrices in c

C Programming
// Addition of Two Matrices 

#include <stdio.h>
// Function to perform matrix addition
void addMatrices(int A[10][10], int B[10][10], int result[10][10], int rows, int cols) {
    int i,j;
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            result[i][j] = A[i][j] + B[i][j];
        }
    }
}

// Function to input matrix elements
void inputMatrix(int matrix[10][10], int rows, int cols) {
    printf("Enter elements of the matrix (%dx%d):\n", rows, cols);
    int i,j;
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

// Function to display a matrix
void displayMatrix(int matrix[10][10], int rows, int cols) {
    int i,j;
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("%2d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int A[10][10], B[10][10], result[10][10];
    int rows, cols;

    // Input matrix dimensions
    printf("Enter the number of rows and columns: ");
    scanf("%d %d", &rows, &cols);

    // Input matrices A and B
    printf("Input matrix A:\n");
    inputMatrix(A, rows, cols);

    printf("Input matrix B:\n");
    inputMatrix(B, rows, cols);

    // Add matrices A and B
    addMatrices(A, B, result, rows, cols);

    // Display the results
    printf("\nMatrix A:\n");
    displayMatrix(A, rows, cols);

    printf("\nMatrix B:\n");
    displayMatrix(B, rows, cols);

    printf("\nResultant Matrix (A + B):\n");
    displayMatrix(result, rows, cols);

    return 0;
}

OUTPUT
Enter the number of rows and columns: 3 3
Input matrix A:
Enter elements of the matrix (3x3):
1 2 3
4 5 6
7 8 9
Input matrix B:
Enter elements of the matrix (3x3):
1 0 0
0 1 0
0 0 1

Matrix A:
 1  2  3 
 4  5  6 
 7  8  9 

Matrix B:
 1  0  0 
 0  1  0 
 0  0  1 

Resultant Matrix (A + B):
 2  2  3 
 4  6  6 
 7  8 10

๐Ÿ’ป Run This C Program

Write a C program that uses functions to perform Addition of Two Matrices
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals