आगे C++ में प्रोग्राम दिया जा रहा है, जो Square Matrix बनाता है तथा उसमें डेटा इनपुट करता है साथ ही Diagonal Elements को प्रिंट करता है
विवरण (Problem Explanation)
-
Square Matrix वह मैट्रिक्स होती है जिसमें Rows = Columns होते हैं।
-
Diagonal Elements वे तत्व होते हैं जिनमें Row index = Column index होता है
(जैसे: a[0][0], a[1][1], a[2][2] …)
C++ प्रोग्राम
#include <iostream>
using namespace std;
int main()
{
int n;
int a[10][10];
// मैट्रिक्स का आकार इनपुट
cout << “Square Matrix का order दर्ज करें: “;
cin >> n;
// मैट्रिक्स के तत्व इनपुट
cout << “Matrix के तत्व दर्ज करें:\n”;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
// पूरी मैट्रिक्स प्रिंट करना
cout << “\nMatrix है:\n”;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout << a[i][j] << ” “;
}
cout << endl;
}
// Diagonal Elements प्रिंट करना
cout << “\nDiagonal Elements हैं:\n”;
for(int i = 0; i < n; i++)
{
cout << a[i][i] << ” “;
}
return 0;
}
उदाहरण (Example)
Input:
1 2 3
4 5 6
7 8 9
Output:
1 5 9

Speak Your Mind