Monday, 5 October 2015

Write a program to add two matrixes.

  
   
#include<stdio.h>
//#include<conio.h>
void main() {
    int a[10][10], b[10][10], c[10][10], i, j, m, n, p, q;
    //clrscr();
    printf("\nProgram to Add Two Matrices\n");
    printf("\nHow Many Rows & Columns of A Matrix: ");
    scanf("%d %d", &n, &m);
    printf("\nHow Many Rows & Columns of B Matrix: ");
    scanf("%d %d", &p, &q);
    if ((n == p)&&(m == q)) {
        printf("\nMatrices Can Be Added");
        printf("\nEnter Elements for A Matrix: \n");
        for (i = 0; i < n; i++) {
            for (j = 0; j < m; j++) {
                scanf("%d", &a[i][j]);
            }
        }
        printf("nEnter Elements for B Matrix: \n");
        for (i = 0; i < p; i++) {
            for (j = 0; j < q; j++) {
                scanf("%d", &b[i][j]);
            }
        }
        for (i = 0; i < n; i++) {
            for (j = 0; j < m; j++) {
                c[i][j] = a[i][j] + b[i][j];
            }
        }
        printf("\nSum of Two Matrices:\n");
        for (i = 0; i < n; i++) {
            for (j = 0; j < m; j++) {
                printf("\n%5d", c[i][j]);
            }
            printf(" ");
        }
    } else
        printf("\nMatrices Cannot Be Added");
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

No comments:

Post a Comment