Skip to main content

C program to read m x n matrix and calculate the Row sum and Column sum of the matrix.

 C program to read m x n matrix and calculate the Row sum and Column sum of the matrix.

    In this example, we read a m x n matrix whose value would be entered by the user then the sum of row and column is calculated. Here in the example we have taken a matrix of order 3 x 2 whose row sum and column sum is displayed in the output screen.

PROGRAM TO READ m x n MATRIX AND CALCULATE THE ROW SUM AND COLUMN SUM OF THE MATRIX:

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,m[100][100],row,column,rsum,csum=0;
    printf("Enter the row size of the matrix :");
    scanf("%d",&row);
    printf("Enter the column size of the matrix :");
    scanf("%d",&column);
    printf("Enter the Matrix : ");
    for(i=0;i<row;i++)
    {
        for(j=0;j<column;j++)
        {
            scanf("%d",&m[i][j]);
        }
    }
    for(i=0;i<row;i++)
    {
        rsum=0;
        for(j=0;j<column;j++)
        {
            rsum=rsum+m[i][j];
        }
        printf("\nSum of row %d : %d",i+1,rsum);
    }
    for(j=0;j<column;j++)
    {
        csum=0;
        for(i=0;i<row;i++)
        {
            csum=csum+m[i][j];
        }
        printf("\nSum of column %d : %d",j+1,csum);
    }
    getch();
}


OUTPUT  TO READ M X N MATRIX AND CALCULATE THE ROW SUM AND COLUMN SUM OF THE MATRIX:

    Enter the row size of the matrix :3

    Enter the column size of the matrix :2

    Enter the Matrix : 12

    34

    65

    7

    23

    9

    

    Sum of row 1 : 46    

    Sum of row 2 : 72    

    Sum of row 3 : 32    

    Sum of column 1 : 100

    Sum of column 2 : 50 


NOTE: You Can Comment Your Code if you have solved differently and we will pin it in the comment section.  Let's Learn together. 


MOTTO: 

                    You Learn, I Learn together We Learn.

Comments

Popular posts from this blog

C Program to input Values in a 2x2 square matrix and display the value

 C Program to input Values in a 2x2 square matrix and display the value     In this example, we will accept values from the user in a 2x2 square and matrix and then it is displayed. To solve this problem we must know what is matrices and arrays. What are Arrays?      An array is a data structure for storing more than one data member of similar data type.          Array can be of following types: One-dimensional Array Two-dimensional Array Multi-dimensional Array             In Array we must always initialize values. PROGRAM #include <stdio.h> #include <conio.h> int   main () {      int   a [ 2 ][ 2 ], i , j ;      printf ( "Enter the values of the matrix:" );      for ( i = 0 ; i < 2 ; i ++)     {          for ( j = 0 ; j < 2 ...

C Program to find the Transpose of a Matrix.

C Program to find the Transpose of  a Matrix.       In this example, we will find the transpose of a given matrix according to the requirements of the row size and column size.      Here in the output we have entered a 3x2 Matrix whose transpose will be 2x3 Matrix. PROGRAM #include <stdio.h> #include <conio.h> int   main () {      int   row , column , i , j , a [ 100 ][ 100 ];      printf ( "Enter the row size of the matrix :" );      scanf ( " %d " ,& row );      printf ( "Enter the column size of the matrix : " );      scanf ( " %d " ,& column );      printf ( "Enter the elements of the matrix : " );      for ( i = 0 ; i < row ; i ++)     {  ...

C Program to find the sum of the elements of a 2-D array.

 C Program to find the sum of the elements of a 2-D array.     In this example, we will find the sum of the elements of a 2-D array allowing the user to enter the row size and column size according to their requiremnts.      Here in the output we have entered a row  size 3 and column size 3. PROGRAM #include <stdio.h> #include <conio.h> void   main () {      int   a [ 50 ][ 50 ], r , c , i , j , sum = 0 ;      printf ( "Enter the row size and column size of the array :" );      scanf ( " %d%d " ,& r ,& c );      printf ( "Enter the elements of the array : " );      for ( i = 0 ; i < r ; i ++)     {          for ( j = 0 ; j < c ; j ++)       ...

C Program to Swap the values of two variables using function.

C Program to Swap the values of two variables using function.     In this example, we swap the values of two variables using function. We have declared a function swap. PROGRAM #include <stdio.h> #include <conio.h> void   swap ( int *, int  *); int   main () {      int   a , b ;      printf ( "Enter the values of a and b : " );      scanf ( " %d%d " ,& a ,& b );      printf ( "Before swapping the values in main a= %d  , b= %d \n " , a , b );      swap (& a ,& b );      printf ( "After swapping the values in main a= %d  , b= %d \n " , a , b );      getch (); } void   swap ( int  * a , int  * b ) {      int   temp ;    ...

C Program to find the sum of the series 1^2+2^2+3^2+…..+n^2

C Program to find the sum of the series  1 2 +2 2 +3 2 +…..+n 2 Enter the end limit at the runtime.       In this example, we calculate the sum of the above mentioned series till the end-limit entered by the user. We have used built-in function pow(x,y) to find the power of the given number.     Here in the output we have entered the value of end-limit as number 7 which gives a sum of 140.    PROGRAM #include <stdio.h> #include <math.h> #include <conio.h> void   main () {      int  n,sum= 0 ,i;      printf ( "Enter the end limit :" );      scanf ( " %d " ,&n);      for (i= 1 ;i<=n;i++)     {         sum=sum+ pow (i, 2 );     }      printf ( "Sum of the series is :  %d " ,sum);  ...