Nested Loops Practice
Loops inside loops — pattern printing, matrix traversal, and nested iteration.
1
Right-Angled Triangle Pattern
Easy
Given an integer N, print a right-angled triangle pattern of asterisks with N rows.
Input Format
A single integer N.
Output Format
Print N rows. Row i should contain i asterisks.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
* ** *** **** |
|
Sample Input 2
1 |
Sample Output 2
* |
|
Sample Input 3
6 |
Sample Output 3
* ** *** **** ***** ****** |
Constraints
1 ≤ N ≤ 50
Explanation
Use outer loop for rows and inner loop for columns. Inner loop runs i times.
2
Square Pattern of Numbers
Easy
Given an integer N, print an N×N square pattern where each cell contains the column number.
Input Format
A single integer N.
Output Format
Print N rows, each containing N numbers separated by spaces. The j-th number in each row should be j.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
5 |
Sample Output 3
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 |
Constraints
1 ≤ N ≤ 20
Explanation
Outer loop for N rows, inner loop for N columns printing the column index.
3
Inverted Right-Angled Triangle
Easy
Given N, print an inverted right-angled triangle pattern. The first row has N stars, and each subsequent row has one fewer star.
Input Format
A single integer N.
Output Format
Print N rows. Row 1 has N stars, row 2 has N-1 stars, ..., row N has 1 star.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
***** **** *** ** * |
|
Sample Input 2
1 |
Sample Output 2
* |
|
Sample Input 3
4 |
Sample Output 3
**** *** ** * |
Constraints
1 ≤ N ≤ 50
Explanation
Outer loop i from N down to 1, inner loop runs i times.
4
Number Triangle
Easy
Row i: number i repeated i times.
Input Format
Single int N
Output Format
N rows.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
1 2 2 3 3 3 4 4 4 4 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
5 |
Sample Output 3
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 |
Constraints
1≤N≤20
Explanation
Nested loops.
5
Same Number Square
Easy
N×N square with row numbers.
Input Format
Single int N
Output Format
N rows of N numbers.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
1 1 1 2 2 2 3 3 3 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
5 |
Sample Output 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 |
Constraints
1≤N≤20
Explanation
Nested loops.
6
Alphabet Square
Easy
N×N square with letters A,B,C...
Input Format
Single int N (1-26)
Output Format
N rows each with N letters.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
A A A A B B B B C C C C D D D D |
|
Sample Input 2
1 |
Sample Output 2
A |
|
Sample Input 3
26 |
Sample Output 3
A A A A A A A A A A A A A A A A A A A A A A A A A A B B B B B B B B B B B B B B B B B B B B B B B B B B C C C C C C C C C C C C C C C C C C C C C C C C C C D D D D D D D D D D D D D D D D D D D D D D D D D D E E E E E E E E E E E E E E E E E E E E E E E E E E F F F F F F F F F F F F F F F F F F F F F F F F F F G G G G G G G G G G G G G G G G G G G G G G G G G G H H H H H H H H H H H H H H H H H H H H H H H H H H I I I I I I I I I I I I I I I I I I I I I I I I I I J J J J J J J J J J J J J J J J J J J J J J J J J J K K K K K K K K K K K K K K K K K K K K K K K K K K L L L L L L L L L L L L L L L L L L L L L L L L L L M M M M M M M M M M M M M M M M M M M M M M M M M M N N N N N N N N N N N N N N N N N N N N N N N N N N O O O O O O O O O O O O O O O O O O O O O O O O O O P P P P P P P P P P P P P P P P P P P P P P P P P P Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q R R R R R R R R R R R R R R R R R R R R R R R R R R S S S S S S S S S S S S S S S S S S S S S S S S S S T T T T T T T T T T T T T T T T T T T T T T T T T T U U U U U U U U U U U U U U U U U U U U U U U U U U V V V V V V V V V V V V V V V V V V V V V V V V V V W W W W W W W W W W W W W W W W W W W W W W W W W W X X X X X X X X X X X X X X X X X X X X X X X X X X Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z |
Constraints
1≤N≤26
Explanation
chr(65+i-1)
7
Odd Number Triangle
Easy
Row i: first i odd numbers.
Input Format
Single int N
Output Format
N rows.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
1 1 3 1 3 5 1 3 5 7 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
5 |
Sample Output 3
1 1 3 1 3 5 1 3 5 7 1 3 5 7 9 |
Constraints
1≤N≤20
Explanation
Counter +=2 each iteration.
8
Binary Triangle
Easy
Row i: i binary digits starting with 1.
Input Format
Single int N
Output Format
N rows.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
1 1 0 1 0 1 1 0 1 0 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
5 |
Sample Output 3
1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 |
Constraints
1≤N≤20
Explanation
(j%2==1) for 1 else 0.
9
Right-Aligned Number Triangle
Easy
Row i: (N-i) spaces then 1..i
Input Format
Single int N
Output Format
N rows right-aligned.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
1 1 2 1 2 3 1 2 3 4 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
5 |
Sample Output 3
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 |
Constraints
1≤N≤20
Explanation
Spaces + numbers loops.
10
V Pattern
Easy
V-shaped asterisk pattern.
Input Format
Single int N (height ≥2)
Output Format
N rows V shape.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
* * * * * * * |
|
Sample Input 2
2 |
Sample Output 2
* * * |
|
Sample Input 3
5 |
Sample Output 3
* *
* *
* *
* *
*
|
Constraints
2≤N≤20
Explanation
Stars at col==i and col==2*N-i.
1
Pyramid Pattern
Medium
Given N, print a centered pyramid pattern of asterisks.
Input Format
A single integer N.
Output Format
Print N rows forming a centered pyramid. Row i should have (N-i) spaces followed by (2*i - 1) asterisks.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
* *** ***** ******* |
|
Sample Input 2
1 |
Sample Output 2
* |
|
Sample Input 3
5 |
Sample Output 3
* *** ***** ******* ********* |
Constraints
1 ≤ N ≤ 30
Explanation
Outer loop for rows. Inner loops: one for spaces (N-i), one for stars (2*i-1).
2
Diamond Pattern
Medium
Given N, print a diamond pattern of asterisks. The diamond should have 2*N - 1 rows.
Input Format
A single integer N (half the height).
Output Format
Print 2*N-1 rows forming a diamond shape centered horizontally.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
* *** ***** *** * |
|
Sample Input 2
1 |
Sample Output 2
* |
|
Sample Input 3
4 |
Sample Output 3
* *** ***** ******* ***** *** * |
Constraints
1 ≤ N ≤ 20
Explanation
First N rows: increasing pyramid (i=1..N). Next N-1 rows: decreasing pyramid (i=N-1..1).
3
Generate Multiplication Table (1 to N)
Medium
Given N, generate and display the multiplication tables from 1 to N.
Input Format
A single integer N.
Output Format
For each number i from 1 to N, print its multiplication table from 1 to 10. Separate each table with a blank line.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9 1 x 10 = 10 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 |
|
Sample Input 2
1 |
Sample Output 2
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9 1 x 10 = 10 |
|
Sample Input 3
2 |
Sample Output 3
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9 1 x 10 = 10 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 |
Constraints
1 ≤ N ≤ 20
Explanation
Outer loop for each number from 1 to N, inner loop for multiplying 1 to 10.
4
Hourglass Pattern
Medium
N rows then N-1 rows mirrored. Centered.
Input Format
Single int N (half)
Output Format
2N-1 rows.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
***** *** * *** ***** |
|
Sample Input 2
2 |
Sample Output 2
*** * *** |
|
Sample Input 3
4 |
Sample Output 3
******* ***** *** * *** ***** ******* |
Constraints
2≤N≤20
Explanation
Decrease then increase.
5
Number Pyramid
Medium
Row i: 1..i then i-1..1, centered.
Input Format
Single int N (1-9)
Output Format
N rows pyramid.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
1 121 12321 1234321 123454321 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
9 |
Sample Output 3
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
|
Constraints
1≤N≤9
Explanation
3 inner loops: spaces, asc, desc.
6
Butterfly Pattern
Medium
Top N rows increasing stars, bottom N mirror.
Input Format
Single int N (half)
Output Format
2N rows.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
* * ** ** *** *** ******** ******** *** *** ** ** * * |
|
Sample Input 2
2 |
Sample Output 2
* * **** **** * * |
|
Sample Input 3
3 |
Sample Output 3
* * ** ** ****** ****** ** ** * * |
Constraints
2≤N≤25
Explanation
Spaces = 2*(N-i) top, 2*i bottom.
7
Matrix Diagonal Sum
Medium
Sum of primary and secondary diagonals.
Input Format
Line1: N. Next N lines: matrix
Output Format
"Primary: S" and "Secondary: S"
Sample Test Cases
|
Sample Input 1
3 1 2 3 4 5 6 7 8 9 |
Sample Output 1
Primary: 15 Secondary: 15 |
|
Sample Input 2
2 1 2 3 4 |
Sample Output 2
Primary: 5 Secondary: 5 |
|
Sample Input 3
1 5 |
Sample Output 3
Primary: 5 Secondary: 5 |
Constraints
1≤N≤100
Explanation
i==j and i+j==N-1
8
Matrix Transpose
Medium
Transpose R×C matrix.
Input Format
Line1: R C. Lines: matrix
Output Format
C rows × R columns.
Sample Test Cases
|
Sample Input 1
2 3 1 2 3 4 5 6 |
Sample Output 1
1 4 2 5 3 6 |
|
Sample Input 2
2 2 1 2 3 4 |
Sample Output 2
1 3 2 4 |
|
Sample Input 3
1 3 1 2 3 |
Sample Output 3
1 2 3 |
Constraints
1≤R,C≤100
Explanation
result[j][i] = matrix[i][j]
9
Matrix Multiplication
Medium
Multiply A(R1×C1) × B(R2×C2) where C1==R2
Input Format
Line1: R1 C1. A. Line: R2 C2. B.
Output Format
Result R1×C2 matrix.
Sample Test Cases
|
Sample Input 1
2 3 1 2 3 4 5 6 3 2 7 8 9 10 11 12 |
Sample Output 1
58 64 139 154 |
|
Sample Input 2
1 1 2 1 1 3 |
Sample Output 2
6 |
|
Sample Input 3
2 2 1 2 3 4 2 2 5 6 7 8 |
Sample Output 3
19 22 43 50 |
Constraints
C1==R2, ≤50
Explanation
Triple nested loop: i,j,k.
10
Checkerboard Pattern
Medium
N×N 0/1 checkerboard.
Input Format
Single int N
Output Format
N rows of N numbers.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 |
|
Sample Input 2
1 |
Sample Output 2
0 |
|
Sample Input 3
5 |
Sample Output 3
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 |
Constraints
1≤N≤20
Explanation
(i+j)%2==0 -> 0 else 1
1
Hollow Square Pattern
Hard
Given N, print a hollow square pattern of asterisks. The boundary of the square should be filled with stars, and the interior should be empty (spaces).
Input Format
A single integer N (side length).
Output Format
Print N rows forming a hollow square of side N.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
***** * * * * * * ***** |
|
Sample Input 2
3 |
Sample Output 2
*** * * *** |
|
Sample Input 3
6 |
Sample Output 3
****** * * * * * * * * ****** |
Constraints
3 ≤ N ≤ 50
Explanation
Print asterisk at positions where i==1, i==N, j==1, or j==N, otherwise print space.
2
Pascal's Triangle
Hard
Given N, print the first N rows of Pascal's Triangle. Each number is the sum of the two numbers directly above it.
Input Format
A single integer N.
Output Format
Print N rows of Pascal's Triangle. Each row should be centered. Values should be separated by spaces.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
6 |
Sample Output 3
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
|
Constraints
1 ≤ N ≤ 20
Explanation
Each row starts and ends with 1. Middle values: row[i][j] = row[i-1][j-1] + row[i-1][j]. Use nested loops.
3
Matrix Addition
Hard
Given two matrices A and B of size R × C, compute their sum (element-wise addition).
Input Format
First line: two integers R and C. Next R lines: matrix A (C integers per line). Next R lines: matrix B (C integers per line).
Output Format
Print the resulting matrix with R rows, each containing C space-separated integers.
Sample Test Cases
|
Sample Input 1
2 3 1 2 3 4 5 6 7 8 9 10 11 12 |
Sample Output 1
8 10 12 14 16 18 |
|
Sample Input 2
1 1 5 7 |
Sample Output 2
12 |
|
Sample Input 3
1 2 1 2 3 4 |
Sample Output 3
4 6 |
Constraints
1 ≤ R, C ≤ 100, -10^6 ≤ elements ≤ 10^6
Explanation
Use nested loops: outer for rows, inner for columns. Result[i][j] = A[i][j] + B[i][j].
4
Spiral Matrix
Hard
N×N spiral clockwise from 1 to N².
Input Format
Single int N
Output Format
N rows spiral matrix.
Sample Test Cases
|
Sample Input 1
3 |
Sample Output 1
1 2 3 8 9 4 7 6 5 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
4 |
Sample Output 3
1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 |
Constraints
1≤N≤20
Explanation
4 boundaries: top, bottom, left, right.
5
Snake Pattern Matrix
Hard
Numbers 1 to N² in snake order (L→R, R→L alternating).
Input Format
Single int N
Output Format
N rows.
Sample Test Cases
|
Sample Input 1
4 |
Sample Output 1
1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
3 |
Sample Output 3
1 2 3 6 5 4 7 8 9 |
Constraints
1≤N≤50
Explanation
Even row L→R, odd R→L.
6
Zigzag Traversal
Hard
Print matrix in zigzag (L→R, R→L per row).
Input Format
Line1: R C. Lines: matrix
Output Format
Elements space-separated.
Sample Test Cases
|
Sample Input 1
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 1
1 2 3 6 5 4 7 8 9 |
|
Sample Input 2
2 2 1 2 3 4 |
Sample Output 2
1 2 4 3 |
|
Sample Input 3
1 3 1 2 3 |
Sample Output 3
1 2 3 |
Constraints
1≤R,C≤100
Explanation
Even row: L→R, odd: R→L
7
Rotate Matrix 90
Hard
Rotate N×N 90° clockwise in-place.
Input Format
Line1: N. Lines: matrix
Output Format
Rotated matrix.
Sample Test Cases
|
Sample Input 1
3 1 2 3 4 5 6 7 8 9 |
Sample Output 1
7 4 1 8 5 2 9 6 3 |
|
Sample Input 2
1 5 |
Sample Output 2
5 |
|
Sample Input 3
2 1 2 3 4 |
Sample Output 3
3 1 4 2 |
Constraints
1≤N≤100
Explanation
Transpose then reverse each row.
8
Magic Square Check
Hard
All rows, cols, diagonals have same sum?
Input Format
Line1: N odd. Lines: matrix
Output Format
"Magic Square" or "Not"
Sample Test Cases
|
Sample Input 1
3 8 1 6 3 5 7 4 9 2 |
Sample Output 1
Magic Square |
|
Sample Input 2
3 2 7 6 9 5 1 4 3 8 |
Sample Output 2
Magic Square |
|
Sample Input 3
2 1 2 2 1 |
Sample Output 3
Not |
Constraints
3≤N≤20, odd
Explanation
Compute expected sum, verify.
9
Saddle Point
Hard
Element = row min AND col max.
Input Format
Line1: N. Lines: matrix
Output Format
"Saddle Point: V at (r,c)" or "No Saddle Point"
Sample Test Cases
|
Sample Input 1
3 3 2 1 5 6 7 9 8 4 |
Sample Output 1
No Saddle Point |
|
Sample Input 2
2 1 2 3 4 |
Sample Output 2
Saddle Point: 3 at (2, 1) |
|
Sample Input 3
3 1 2 3 4 5 6 7 8 9 |
Sample Output 3
Saddle Point: 7 at (3, 1) |
Constraints
1≤N≤100
Explanation
Find min per row, check if max in col.
10
Boundary Sum
Hard
Sum of first/last row and column elements (no double-count corners).
Input Format
Line1: R C. Lines: matrix
Output Format
Sum int.
Sample Test Cases
|
Sample Input 1
4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Sample Output 1
102 |
|
Sample Input 2
2 2 1 2 3 4 |
Sample Output 2
10 |
|
Sample Input 3
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 3
40 |
Constraints
2≤R,C≤100
Explanation
Add i==0, i==R-1, j==0, j==C-1, skip corners twice.