pogram to find the sum of left and right diagonal using array
UtkarshIshwar:
In which language do you need the program? And by diagonal do you mean diagonals of a matrix?
Answers
Answered by
1
#include<iostream>#include<conio.h>using namespace std;
int matrix[4][4];
int getarray() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { std::cout <<"enter row "<<i+1<<" element "<<j+1<<endl; std::cin >> matrix[i][j]; } } return 0; }
int showarray() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { std::cout <<matrix[i][j]<<'\t'; } std::cout << '\n'; } return 0; }
int diagsum() { int sum1=0,sum2=0; for(int i=0;i<4;i++) { sum1+=matrix[i][i]; } for (int i = 4; i > 0; i--) { sum2+=matrix[i][3-i]; } std::cout<<"\n\nSum of 1st diagonal : "<<sum1; std::cout<<"\nSum of 2nd diagonal : "<<sum2; return 0; }
int main() { std::cout << "Enter the array :" << '\n'; getarray(); std::cout << "\n\nThe array is :\n\n" << '\n'; showarray(); diagsum(); getch(); return 0; }
int matrix[4][4];
int getarray() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { std::cout <<"enter row "<<i+1<<" element "<<j+1<<endl; std::cin >> matrix[i][j]; } } return 0; }
int showarray() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { std::cout <<matrix[i][j]<<'\t'; } std::cout << '\n'; } return 0; }
int diagsum() { int sum1=0,sum2=0; for(int i=0;i<4;i++) { sum1+=matrix[i][i]; } for (int i = 4; i > 0; i--) { sum2+=matrix[i][3-i]; } std::cout<<"\n\nSum of 1st diagonal : "<<sum1; std::cout<<"\nSum of 2nd diagonal : "<<sum2; return 0; }
int main() { std::cout << "Enter the array :" << '\n'; getarray(); std::cout << "\n\nThe array is :\n\n" << '\n'; showarray(); diagsum(); getch(); return 0; }
Attachments:
Similar questions