Matrix Addition - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void main()
{
    int a[100][100],b[100][100],c[100][100],i,j,m,p,q,n;
    printf("Enter the order for first matrix:");
    scanf("%d %d",&m,&n);
    printf("Enter the elements for first matrix:");
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
        scanf("%d",&a[i][j]);
    printf("Enter the order for second matrix:");
    scanf("%d %d",&p,&q);
    printf("Enter the elements for second matrix:");
    for(i=0;i<p;i++)
    for(j=0;j<q;j++)
    scanf("%d",&b[i][j]);
   
    if(m==p && p==q)
    {
        for(i=0;i<p;i++)
        for(j=0;j<q;j++)
            c[i][j]=a[i][j]+b[i][j];
        printf("Resultant matrix:\n");
        for(i=0;i<p;i++)
        {
            for(j=0;j<q;j++)
                printf("%d ",c[i][j]);
                printf("\n");
        }
    }
    else
        printf("Operation not possible.\n");
//getch();
}

Output:
nn@linuxmint ~ $ gcc c8.c
nn@linuxmint ~ $ ./a.out
Enter the order for first matrix:2
2
Enter the elements for first matrix:1
1
1
1
Enter the order for second matrix:2
2
Enter the elements for second matrix:2
2
2
2
Resultant matrix:
3 3
3 3
nn@linuxmint ~ $

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...