Sample – Program to find the sum of either of the diagonals of a 4 x 4 matrix.

by Nideesh C on February 8, 2011 · 0 comments

in C++




#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int x;
int A[4][4],sum=0; //Reading the matrix.
cout << “Enter the elements of the matrix : ” << endl;
for(int y=0;y<4;y++)
for (int x=0;x<4;x++)
{
cout << “Element ” << x+1 << “, ” << y+1 << ” : “;
cin>>A[x][y];
}
//Sum of either of the diagonal elements.
for(x=0;x<4;x++)
for(y=0;y<4;y++)
if(x==y)
sum+=A[x][y];
else if(y==4-(1+1));
sum+=A[x][y];
cout << “Sum of either of the diagonal elements is : ” << sum;
getch();
}

This program takes in the elements A[x][y] of the 4 x 4 matrix as a screen input from the user.
It then calculates the sum of either of its diagonals and outputs it using the ‘cout’ command.

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..
  2. Sample – Program to enter an integer and find out if it is even or odd.
  3. Sample – Program to find the roots of a quadratic equation.
  4. Sample – Program to enter a string and find its length.
  5. Sample – Program to find the total days in the year till date.

Leave a Comment

Previous post:

Next post: