w3resource

C Exercises: Find sum of right diagonals of a matrix

C Array: Exercise-23 with Solution

Write a program in C to find the sum of the right diagonals of a matrix.

Pictorial Presentation:

C Exercises: Find sum of right diagonals of a matrix

Sample Solution:

C Code:

#include <stdio.h>

void main()
 
   {
     int i,j,arr1[50][50],sum=0,n;
	 
       printf("\n\nFind sum of right diagonals of a matrix :\n");
       printf("---------------------------------------\n");	 
	 
	 printf("Input the size of the square matrix : ");
     scanf("%d", &n); 
	 printf("Input elements in the first matrix :\n");
       for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
	           printf("element - [%d],[%d] : ",i,j);
	           scanf("%d",&arr1[i][j]);
			   if (i==j) sum= sum+arr1[i][j];
            }
        }  
	 

	 printf("The matrix is :\n");
	 for(i=0;i<n;i++)
	 {
	   for(j=0;j<n ;j++)
	     printf("% 4d",arr1[i][j]);
	    printf("\n");
	 }
 
       printf("Addition of the right Diagonal elements is :%d\n",sum);
    }

Sample Output:

Find sum of right diagonals of a matrix :                                                                     
---------------------------------------                                                                       
Input the size of the square matrix : 2                                                                       
Input elements in the first matrix :                                                                          
element - [0],[0] : 1                                                                                         
element - [0],[1] : 2                                                                                         
element - [1],[0] : 3                                                                                         
element - [1],[1] : 4                                                                                         
The matrix is :                                                                                               
   1   2                                                                                                      
   3   4                                                                                                      
Addition of the right Diagonal elements is :5

Flowchart:

Flowchart: Find sum of right diagonals of a matrix

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous:> Write a program in C to find transpose of a given matrix.
Next: Write a program in C to find sum of left diagonals of a matrix.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP