23.12.19

Sum of two Matrices program in c

#include <stdio.h>


void main()


{


 int r1,c1,r2,c2,c,d,f[10][10],s[10][10],sum[10][10];


 printf("Enter the number of rows and columns of First Matrix\n");


 scanf("%d%d",&r1,&c1);


 printf("Enter the number of rows and columns of Second Matrix\n");


 scanf("%d%d",&r2,&c2);


 if(r1==r2 && c1==c2)


 {


 printf("Enter the elements of First Matrix\n");

 for(c=0;c<r1;c++)


    for(d=0;d<c1;d++)


       scanf("%d",&f[c][d]);


 printf("Enter the elements of Second Matrix\n");


 for(c=0;c<r2;c++)


    for(d=0;d<c2;d++)


        scanf("%d",&s[c][d]);


 printf("Sum of Two Matrices:\n");


 for(c=0;c<r1;c++)  //we may use r1,c1 combinatio or r2,c2.


 {


  for(d=0;d<c1;d++)


     {


       sum[c][d]=f[c][d]+s[c][d];


       printf("%d\t", sum[c][d]);


     }


  printf("\n");


  }


 }


 else
 {


  printf("Matrix addition is not possible...\n");


 }


}




Output1 :
Enter the number of rows and columns of First Matrix


2
2


Enter the number of rows and columns of Second Matrix


2
2


Enter the elements of First Matrix


1
2
3
4


Enter the elements of Second Matrix


1
2
3
4


Sum of Two Matrices:
2    4   
6    8   

No comments:

Post a Comment

JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...