Write a c++ program to find sum of diagonal elements of a square matrix
Write a c++ program to find sum of diagonal elements of a square matrix
//
// main.cpp
// SumOfDiagonal
//
// Created by SHIVAM SINGH on 23/07/22.
//
#include <iostream>
using namespace std;
class sqr_mat
{
int arr[5][5];
public :
void input( );
int diag_sum( );
void display( );
};
void sqr_mat :: input()
{
int i,j;
for (i = 0 ; i < 5 ; i++ ) {
for (j = 0 ; j < 5 ; j++ ) {
cout<<“Input a number : “<<endl;
cin>>arr[i][j];
}
}
}
void sqr_mat :: display()
{
for (int i = 0 ; i < 5 ; i++ ) {
for (int j = 0 ; j < 5 ; j++ ) {
cout<<arr[i][j]<<” “;
}
cout<<endl;
}
}
int sqr_mat :: diag_sum()
{
int i,j,sum=0,t=4;
for (i = 0 ; i < 5 ; i++ ) {
for (j = 0 ; j < 5 ; j++ ) {
if ( i == j ) {
sum = sum + arr[i][j];
}
}
sum = sum + arr[i][t];
t–;
}
sum = sum – arr[2][2];
return sum;
}
int main()
{
sqr_mat obj;
obj.input();
obj.display();
cout<<“Sum of Diagonal is : “<< obj.diag_sum()<<endl;
return 0;
}
Topics Covered in this blog :
- c program to print diagonal elements of array
- diagonal elements of an matrix
- sum of diagonal elements
- sum of both diagonals of matrix in c
- c programming language
- two dimensional arrays
- programming
- c programming
- programming c
- row index
- column index
- software
- matrix
- c files
- c training
- c language for beginnersk
- anthety
- compiler
- c language
- c compiler
- object code
- computer basics
- arrays
- binary
- coding
- sundeep
- 2d arrays
- c code
- saradhi
- exe code
- .exe
Thanks for reading this blog. Hope you get satisfied with the blog and definitely this blog must have valued your time and effort of reading.
Take a time to connect our other digital creations such as Instagram , Facebook and Youtube.
Social Media Links of Tech DCode :
YouTube : https://www.youtube.com/channel/UCjJnEdeugftBwQ3yMuD4B_A
Instagram : https://www.instagram.com/thetechdcode/
Facebook Page : https://www.facebook.com/thetechdcode
Twitter : https://twitter.com/thetechdcode
Telegram Channel : https://t.me/thetechdcode
Tech DCode Linktree : https://linktr.ee/thetechdcode
My Personal Handles : https://linktr.ee/virtualshivamin
Social Media Links of SHIVAM SINGH (OWNER) :
Instagram : https://www.instagram.com/virtualshivamin/
Facebook Page : https://www.facebook.com/virtualshivamin/
Twitter : https://twitter.com/virtualshivamin/
Personal Linktree : https://linktr.ee/virtualshivamin