C++ Online Compiler
Example: Upper Triangular Matrix (Hardcoded) in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Upper Triangular Matrix (Hardcoded) #include <iostream> using namespace std; int main() { // Step 1: Define a sample 3x3 matrix int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int rows = 3; int cols = 3; cout << "Original Matrix:\n"; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cout << matrix[i][j] << " "; } cout << endl; } cout << "\nUpper Triangular Matrix:\n"; // Step 2: Iterate through the matrix to display its upper triangular form for (int i = 0; i < rows; ++i) { // Outer loop for rows for (int j = 0; j < cols; ++j) { // Inner loop for columns // Step 3: Check if the current element is below the main diagonal if (j < i) { cout << "0 "; // If below diagonal, print 0 } else { cout << matrix[i][j] << " "; // Otherwise, print original element } } cout << endl; // Move to the next line after each row } return 0; }
Output
Clear
ADVERTISEMENTS