Posts

Showing posts from October, 2023

Calculating the sum of diagonal elements and then finding the difference in C++

Image
                            #add of diagonal of 2d array                           #include <iostream> using namespace std; #include <math.h> int main() {     int arr[3][3] = {1, 4, 2, 5, 2, 8, 2, 5, 9};     int sum1 = 0 , sum2 = 0;      for (int i = 0; i < 3; i++) {         sum1 += arr[i][i];          sum2 += arr[i][2 - i];      }     int d = abs(sum1 - sum2);     cout << "Sum of diagonal1: " << sum1 << endl;     cout << "Sum of diagonal2: " << sum2 << endl;     cout << "Diff between the sums: " << d << endl;     return 0; }