C Online Compiler
Example: C Program to Print Matrix Pattern using For loop
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C Program to Print Matrix Pattern using For loop #include <stdio.h> int main() { int x, i, j, k, d = 1, m; // x - To store the number of rows printf("-----Enter the total number of rows-----\n"); scanf("%d", &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++; } } for ( i = 0; i < x; i++) { for(j = 0; j < x; j++) { printf("%d ", mat[i][j]); } printf("\n"); } return 0; }
6
Output
Clear
ADVERTISEMENTS