#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
int n;
cout << "Enter the order of square matrix: ";
cin >> n;
double a[n][n + 1], x[n];
cout << "Enter the elements of augmented matrix row-wise:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
cin >> a[i][j];
}
}
// Forward Elimination to get upper triangular matrix
for (int i = 0; i < n; i++) {
// Partial pivoting (optional but improves accuracy)
for (int k = i + 1; k < n; k++) {
if (fabs(a[i][i]) < fabs(a[k][i])) {
for (int j = 0; j <= n; j++) {
swap(a[i][j], a[k][j]);
}
}
}
for (int k = i + 1; k < n; k++) {
double factor = a[k][i] / a[i][i];
for (int j = 0; j <= n; j++) {
a[k][j] -= factor * a[i][j];
}
}
}
// Backward Substitution
for (int i = n - 1; i >= 0; i--) {
x[i] = a[i][n];
for (int j = i + 1; j < n; j++) {
x[i] -= a[i][j] * x[j];
}
x[i] /= a[i][i];
}
// Output the solution
cout << "\nThe solution is:\n";
for (int i = 0; i < n; i++) {
cout << "x" << i + 1 << " = " << fixed << setprecision(6) << x[i] << endl;
}
return 0;
}