C++ Online Compiler
Example: Matrix Pattern in C++ language using For loop
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Matrix Pattern in C++ language using For loop #include <iostream> using namespace std; int main() { int x, i, j, k, d = 1, m; // x - denotes the number of rows cout << "-----Enter the total number of rows-----\n"; cin >> x; int mat[x][x]; for ( i = 0; i < x; i++) { j = i; k = 0; for ( j = i; j >= 0; j--) { mat[k][j] = d; d++; k++; } } for ( k = 1; k < x; k++) { i = m = k; for ( j = x-1; j >= m; j--) { mat[i][j] = d; d++; i++; } } cout << "\n-----The matrix pattern-----\n\n"; for ( i = 0; i < x; i++) { for(j = 0; j < x; j++) { cout << mat[i][j] << " "; } cout << "\n"; } return 0; }
5
Output
Clear
ADVERTISEMENTS