Lets write a simple C program to print/display a 3×5 matrix using nested for loop.
Note:
3×5 matrix means, a Matrix with 3 rows and 5 columns.
Related Read:
Nested For Loop In C Programming Language
Outer for loop selects the rows. Inner for loop prints elements of that row. Next outer for loop selects the next row, and the inner for loop prints elements for that selected row. This continues until all the elements of the Matrix are printed.
Video Tutorial: C Program To Print Matrix using Nested For Loop
#include<stdio.h> int main() { int row, col; for(row = 1; row <= 3; row++) { for(col = 1; col <= 5; col++) { printf("\t%d", col); } printf("\n"); } return 0; }
Output:
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert