0% found this document useful (0 votes)
4 views167 pages

Nested Loop Practice Part 2

The document is a comprehensive masterclass on nested loops and patterns in C programming, designed to take learners from beginner to advanced levels. It includes foundational concepts, magic questions for pattern creation, and a variety of coding examples for different patterns such as triangles, squares, and pyramids. The content is structured in levels, progressively introducing more complex patterns and their corresponding C code implementations.

Uploaded by

nibir2341
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views167 pages

Nested Loop Practice Part 2

The document is a comprehensive masterclass on nested loops and patterns in C programming, designed to take learners from beginner to advanced levels. It includes foundational concepts, magic questions for pattern creation, and a variety of coding examples for different patterns such as triangles, squares, and pyramids. The content is structured in levels, progressively introducing more complex patterns and their corresponding C code implementations.

Uploaded by

nibir2341
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LMArena

🔥 COMPLETE NESTED LOOPS & PATTERNS


MASTERCLASS 🔥
From ZERO to HERO (Level 1-100) in C Language

📚 PART 1: THE FOUNDATION (Level 0)


Understanding the GOLDEN RULE First

text
╔════════════════════════════════════════════════════════════════╗
║ UNIVERSAL PATTERN FORMULA (Remember This Forever!) ║
║ ║
║ Every Pattern = ROWS × (SPACES + SYMBOLS) ║
║ ║
║ for(row = 1; row <= n; row++) ← Controls HEIGHT ║
║ { ║
║ for(space = 1; space <= ?; space++) ← Controls GAPS ║
║ printf(" "); ║
║ for(col = 1; col <= ?; col++) ← Controls WIDTH ║
║ printf("*"); ║
║ printf("\n"); ← New Line ║
║ } ║
╚════════════════════════════════════════════════════════════════╝

🎯 THE 5 MAGIC QUESTIONS (Ask Before Every Pattern)


text
┌─────────────────────────────────────────────────────────────┐
│ Q1. How many ROWS? → Outer loop count │
│ Q2. How many SPACES before symbols? → First inner loop │
│ Q3. How many SYMBOLS to print? → Second inner loop │
│ Q4. What SYMBOL to print? → *, numbers, alphabets │
│ Q5. Is there any PATTERN in spaces/symbols? → Find formula │
└─────────────────────────────────────────────────────────────┘

📊 THE RELATIONSHIP TABLE (Memorize This!)


text
For n = 5 rows:
Row(i) │ Spaces(n-i) │ Stars(i) │ Stars(2i-1) │ Stars(n-i+1)
────────┼─────────────┼──────────┼─────────────┼──────────────
1 │ 4 │ 1 │ 1 │ 5
2 │ 3 │ 2 │ 3 │ 4
3 │ 2 │ 3 │ 5 │ 3
4 │ 1 │ 4 │ 7 │ 2
5 │ 0 │ 5 │ 9 │ 1

FORMULAS:
- Increasing stars: j <= i
- Decreasing stars: j <= n-i+1
- Pyramid stars: j <= 2*i-1
- Inverted pyramid stars: j <= 2*(n-i)+1
- Leading spaces for pyramid: n-i

🌟 PART 2: LEVEL 1-10 (Absolute Basics)

LEVEL 1: Simple Horizontal Line


Concept: Single loop prints elements horizontally
C
#include <stdio.h>

int main() {
int n = 5;

// Single loop - prints in ONE line


for(int i = 1; i <= n; i++) {
printf("* ");
}

return 0;
}

Output:

text
* * * * *

Logic: Loop runs 5 times → prints 5 stars in same line

LEVEL 2: Simple Vertical Line


Concept: \n inside loop creates vertical output

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


printf("*\n"); // \n moves to next line after each star
}

return 0;
}

Output:

text
*
*
*
*
*

LEVEL 3: Square Pattern (First Nested Loop!)


Concept: Outer loop = rows, Inner loop = columns

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) { // Row controller


for(int j = 1; j <= n; j++) { // Column controller
printf("* ");
}
printf("\n"); // Move to next row
}

return 0;
}
Output:

text
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Visual Understanding:

text
i=1: j runs 1→5 → prints 5 stars → \n
i=2: j runs 1→5 → prints 5 stars → \n
i=3: j runs 1→5 → prints 5 stars → \n
i=4: j runs 1→5 → prints 5 stars → \n
i=5: j runs 1→5 → prints 5 stars → \n

LEVEL 4: Right-Angled Triangle (Increasing)


Concept: Stars depend on row number: j <= i
C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) { // j goes up to i, not n
printf("* ");
}
printf("\n");
}

return 0;
}

Output:

text
*
* *
* * *
* * * *
* * * * *

Row-by-Row Analysis:

text
Row 1 (i=1): j runs 1 to 1 → 1 star
Row 2 (i=2): j runs 1 to 2 → 2 stars
Row 3 (i=3): j runs 1 to 3 → 3 stars
Row 4 (i=4): j runs 1 to 4 → 4 stars
Row 5 (i=5): j runs 1 to 5 → 5 stars

LEVEL 5: Right-Angled Triangle (Decreasing)


Concept: Stars decrease: j <= n-i+1

C
#include <stdio.h>

int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i+1; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

Output:

text
* * * * *
* * * *
* * *
* *
*

Formula Breakdown:

text
Row 1: n-i+1 = 5-1+1 = 5 stars
Row 2: n-i+1 = 5-2+1 = 4 stars
Row 3: n-i+1 = 5-3+1 = 3 stars
Row 4: n-i+1 = 5-4+1 = 2 stars
Row 5: n-i+1 = 5-5+1 = 1 star

LEVEL 6: Number Pattern (Sequential in Row)


Concept: Print j instead of *

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
printf("%d ", j); // Print column number
}
printf("\n");
}
return 0;
}

Output:

text
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

LEVEL 7: Number Pattern (Same Number in Row)


Concept: Print i instead of j

C
#include <stdio.h>

int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
printf("%d ", i); // Print row number
}
printf("\n");
}

return 0;
}

Output:

text
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

LEVEL 8: Alphabet Pattern (A B C...)


Concept: 'A' + j - 1 gives alphabets
C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
printf("%c ", 'A' + j - 1); // ASCII trick!
}
printf("\n");
}

return 0;
}

Output:

text
A
A B
A B C
A B C D
A B C D E

ASCII Logic:

text
'A' = 65
'A' + 0 = 65 = 'A'
'A' + 1 = 66 = 'B'
'A' + 2 = 67 = 'C'
...and so on

LEVEL 9: Same Alphabet in Each Row

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
printf("%c ", 'A' + i - 1); // Row-based alphabet
}
printf("\n");
}

return 0;
}

Output:

text
A
B B
C C C
D D D D
E E E E E

LEVEL 10: Reverse Number in Row

C
#include <stdio.h>
int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = i; j >= 1; j--) { // j goes from i to 1
printf("%d ", j);
}
printf("\n");
}

return 0;
}

Output:

text
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
🌟 PART 3: LEVEL 11-25 (Introducing Spaces)

🎯 THE SPACE FORMULA CHART


text
╔═══════════════════════════════════════════════════════════════╗
║ PATTERN TYPE │ SPACE FORMULA │ STAR FORMULA ║
╠═══════════════════════════════════════════════════════════════╣
║ Right-aligned ▲ │ spaces = n - i │ stars = i ║
║ Pyramid △ │ spaces = n - i │ stars = 2*i - 1 ║
║ Inverted Pyramid ▽ │ spaces = i - 1 │ stars = 2*(n-i)+1║
║ Diamond ◇ │ Combine both │ Combine both ║
╚═══════════════════════════════════════════════════════════════╝

LEVEL 11: Right-Aligned Triangle


Concept: Add spaces before stars

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// First: Print spaces
for(int j = 1; j <= n-i; j++) {
printf(" "); // 2 spaces (to match "* ")
}
// Then: Print stars
for(int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

Output:

text
*
* *
* * *
* * * *
* * * * *

Visual Formula:

text
Row 1: (5-1)=4 spaces, 1 star
Row 2: (5-2)=3 spaces, 2 stars
Row 3: (5-3)=2 spaces, 3 stars
Row 4: (5-4)=1 space, 4 stars
Row 5: (5-5)=0 spaces, 5 stars

LEVEL 12: Right-Aligned Inverted Triangle

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Spaces increase: i-1
for(int j = 1; j <= i-1; j++) {
printf(" ");
}
// Stars decrease: n-i+1
for(int j = 1; j <= n-i+1; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

Output:

text
* * * * *
* * * *
* * *
* *
*
LEVEL 13: Pyramid Pattern ⭐
THE MOST IMPORTANT PATTERN - MASTER THIS!

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Spaces: n-i
for(int j = 1; j <= n-i; j++) {
printf(" ");
}
// Stars: 2*i-1
for(int j = 1; j <= 2*i-1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}

Output:

text
*
***
*****
*******
*********

Complete Analysis:

text
Row │ Spaces(n-i) │ Stars(2i-1) │ Total Width
────┼─────────────┼─────────────┼─────────────
1 │ 4 │ 1 │ 5
2 │ 3 │ 3 │ 6
3 │ 2 │ 5 │ 7
4 │ 1 │ 7 │ 8
5 │ 0 │ 9 │ 9
LEVEL 14: Inverted Pyramid

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Spaces: i-1
for(int j = 1; j <= i-1; j++) {
printf(" ");
}
// Stars: 2*(n-i)+1
for(int j = 1; j <= 2*(n-i)+1; j++) {
printf("*");
}
printf("\n");
}

return 0;
}
Output:

text
*********
*******
*****
***
*

LEVEL 15: Number Pyramid

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n-i; j++) {
printf(" ");
}
for(int j = 1; j <= i; j++) {
printf("%d", j);
}
for(int j = i-1; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}

return 0;
}

Output:

text
1
121
12321
1234321
123454321

LEVEL 16: Floyd's Triangle


Concept: Continuous counting across rows

C
#include <stdio.h>

int main() {
int n = 5;
int num = 1; // Counter starts at 1

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
printf("%d ", num);
num++; // Increment after each print
}
printf("\n");
}

return 0;
}

Output:
text
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

LEVEL 17: Pascal's Triangle (Formula Based)


Concept: C(n,r) = n! / (r! * (n-r)!)

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 0; i < n; i++) {


// Print spaces
for(int j = 0; j < n-i-1; j++) {
printf(" ");
}
int val = 1;
for(int j = 0; j <= i; j++) {
printf("%4d", val);
val = val * (i - j) / (j + 1); // Pascal formula
}
printf("\n");
}

return 0;
}

Output:

text
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

LEVEL 18: Binary Triangle (0-1 Pattern)


Concept: Alternating based on position

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
// (i+j) % 2 alternates the pattern
if((i + j) % 2 == 0)
printf("1 ");
else
printf("0 ");
}
printf("\n");
}

return 0;
}

Output:
text
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

LEVEL 19: Hollow Square


Concept: Print star only at boundaries

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n; j++) {
// Boundary condition: first row, last row, first col, last col
if(i == 1 || i == n || j == 1 || j == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
* * * * *
* *
* *
* *
* * * * *

LEVEL 20: Hollow Triangle

C
#include <stdio.h>
int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
// First col, last col, or last row
if(j == 1 || j == i || i == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
*
* *
* *
* *
* * * * *

LEVEL 21: Hollow Pyramid

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Print leading spaces
for(int j = 1; j <= n-i; j++) {
printf(" ");
}
// Print stars/spaces
for(int j = 1; j <= 2*i-1; j++) {
if(j == 1 || j == 2*i-1 || i == n)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
*
* *
* *
* *
*********

LEVEL 22: Diamond Pattern ⭐⭐


Concept: Upper pyramid + Lower inverted pyramid
C
#include <stdio.h>

int main() {
int n = 5;

// Upper half (including middle)


for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++)
printf("*");
printf("\n");
}

// Lower half
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++)
printf("*");
printf("\n");
}
return 0;
}

Output:

text
*
***
*****
*******
*********
*******
*****
***
*

LEVEL 23: Hollow Diamond

C
#include <stdio.h>
int main() {
int n = 5;

// Upper half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++) {
if(j == 1 || j == 2*i-1)
printf("*");
else
printf(" ");
}
printf("\n");
}

// Lower half
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++) {
if(j == 1 || j == 2*i-1)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
*
* *
* *
* *
* *
* *
* *
* *
*

LEVEL 24: Sandglass Pattern


Concept: Inverted pyramid + Pyramid

C
#include <stdio.h>

int main() {
int n = 5;

// Upper inverted pyramid


for(int i = 1; i <= n; i++) {
for(int j = 1; j < i; j++)
printf(" ");
for(int j = 1; j <= 2*(n-i)+1; j++)
printf("*");
printf("\n");
}

// Lower pyramid
for(int i = 2; i <= n; i++) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++)
printf("*");
printf("\n");
}

return 0;
}

Output:

text
*********
*******
*****
***
*
***
*****
*******
*********

LEVEL 25: Arrow Pattern

C
#include <stdio.h>
int main() {
int n = 5;

// Upper part
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++)
printf("*");
printf("\n");
}

// Lower part
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= i; j++)
printf("*");
printf("\n");
}

return 0;
}

Output:
text
*
**
***
****
*****
****
***
**
*

🌟 PART 4: LEVEL 26-50 (Intermediate Patterns)

LEVEL 26: Right Arrow

C
#include <stdio.h>

int main() {
int n = 5;
// Upper part
for(int i = 1; i <= n; i++) {
for(int j = 1; j < i; j++)
printf(" ");
for(int j = i; j <= n; j++)
printf("*");
printf("\n");
}

// Lower part
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j < i; j++)
printf(" ");
for(int j = i; j <= n; j++)
printf("*");
printf("\n");
}

return 0;
}

Output:
text
*****
****
***
**
*
**
***
****
*****

LEVEL 27: Plus Pattern

C
#include <stdio.h>

int main() {
int n = 5; // Should be odd
int mid = n / 2;

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
if(i == mid || j == mid)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
*
*
*****
*
*

LEVEL 28: X Pattern


C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
if(i == j || i + j == n - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
* *
* *
*
* *
* *

LEVEL 29: Butterfly Pattern ⭐⭐

C
#include <stdio.h>

int main() {
int n = 5;

// Upper half
for(int i = 1; i <= n; i++) {
// Left stars
for(int j = 1; j <= i; j++)
printf("*");
// Middle spaces
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
// Right stars
for(int j = 1; j <= i; j++)
printf("*");
printf("\n");
}

// Lower half
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= i; j++)
printf("*");
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
for(int j = 1; j <= i; j++)
printf("*");
printf("\n");
}

return 0;
}

Output:

text
* *
** **
*** ***
**** ****
**********
**********
**** ****
*** ***
** **
* *

LEVEL 30: Hollow Butterfly

C
#include <stdio.h>

int main() {
int n = 5;

// Upper half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
if(j == 1 || j == i)
printf("*");
else
printf(" ");
}
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
for(int j = 1; j <= i; j++) {
if(j == 1 || j == i)
printf("*");
else
printf(" ");
}
printf("\n");
}

// Lower half
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= i; j++) {
if(j == 1 || j == i)
printf("*");
else
printf(" ");
}
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
for(int j = 1; j <= i; j++) {
if(j == 1 || j == i)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
* *
** **
* * * *
* * * *
* ** *
* ** *
* * * *
* * * *
** **
* *

LEVEL 31: Rhombus Pattern

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Leading spaces
for(int j = 1; j <= n-i; j++)
printf(" ");
// Stars (constant width)
for(int j = 1; j <= n; j++)
printf("*");
printf("\n");
}
return 0;
}

Output:

text
*****
*****
*****
*****
*****

LEVEL 32: Parallelogram

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Spaces increase
for(int j = 1; j < i; j++)
printf(" ");
// Constant stars
for(int j = 1; j <= n; j++)
printf("*");
printf("\n");
}

return 0;
}

Output:

text
*****
*****
*****
*****
*****

LEVEL 33: Zigzag Pattern


C
#include <stdio.h>

int main() {
int n = 9;

for(int i = 1; i <= 3; i++) {


for(int j = 1; j <= n; j++) {
if(((i + j) % 4 == 0) || (i == 2 && j % 4 == 0))
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
* *
* * * *
* * *

LEVEL 34: Number Diamond

C
#include <stdio.h>

int main() {
int n = 5;

// Upper half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= i; j++)
printf("%d", j);
for(int j = i-1; j >= 1; j--)
printf("%d", j);
printf("\n");
}
// Lower half
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= i; j++)
printf("%d", j);
for(int j = i-1; j >= 1; j--)
printf("%d", j);
printf("\n");
}

return 0;
}

Output:

text
1
121
12321
1234321
123454321
1234321
12321
121
1

LEVEL 35: Alphabet Diamond

C
#include <stdio.h>

int main() {
int n = 5;

// Upper half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++)
printf("%c", 'A' + i - 1);
printf("\n");
}

// Lower half
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++)
printf("%c", 'A' + i - 1);
printf("\n");
}

return 0;
}

Output:

text
A
BBB
CCCCC
DDDDDDD
EEEEEEEEE
DDDDDDD
CCCCC
BBB
A
LEVEL 36: Heart Pattern ❤️

C
#include <stdio.h>

int main() {
int n = 6;

// Upper part (two half circles)


for(int i = n/2; i <= n; i += 2) {
// Left spaces
for(int j = 1; j < n-i; j += 2)
printf(" ");
// Left curve
for(int j = 1; j <= i; j++)
printf("*");
// Middle spaces
for(int j = 1; j <= n-i; j++)
printf(" ");
// Right curve
for(int j = 1; j <= i; j++)
printf("*");
printf("\n");
}

// Lower part (inverted triangle)


for(int i = n; i >= 1; i--) {
for(int j = i; j < n; j++)
printf(" ");
for(int j = 1; j <= (i*2)-1; j++)
printf("*");
printf("\n");
}

return 0;
}

Output:

text
*** ***
***** *****
***********
*********
*******
*****
***
*

LEVEL 37: Spiral Number Pattern

C
#include <stdio.h>

int main() {
int n = 5;
int a[10][10];
int num = 1;
int top = 0, bottom = n-1, left = 0, right = n-1;

while(top <= bottom && left <= right) {


// Top row
for(int i = left; i <= right; i++)
a[top][i] = num++;
top++;

// Right column
for(int i = top; i <= bottom; i++)
a[i][right] = num++;
right--;

// Bottom row
for(int i = right; i >= left; i--)
a[bottom][i] = num++;
bottom--;

// Left column
for(int i = bottom; i >= top; i--)
a[i][left] = num++;
left++;
}

// Print matrix
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
printf("%3d", a[i][j]);
printf("\n");
}

return 0;
}
Output:

text
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

LEVEL 38: Number Wave Pattern

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Increasing part
for(int j = 1; j <= i; j++)
printf("%d", j);
// Decreasing part
for(int j = i-1; j >= 1; j--)
printf("%d", j);
printf("\n");
}

return 0;
}

Output:

text
1
121
12321
1234321
123454321

LEVEL 39: Mirrored Number Triangle

C
#include <stdio.h>
int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Left part (decreasing from n)
for(int j = n; j > i; j--)
printf(" ");
// Numbers
for(int j = i; j >= 1; j--)
printf("%d ", j);
for(int j = 2; j <= i; j++)
printf("%d ", j);
printf("\n");
}

return 0;
}

Output:

text
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5

LEVEL 40: Multiplication Table Pattern

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n; j++) {
printf("%4d", i * j);
}
printf("\n");
}

return 0;
}
Output:

text
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

LEVEL 41: Triangle with Border Numbers

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
if(j == 1 || j == i || i == n)
printf("%d ", j);
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
1
1 2
1 3
1 4
1 2 3 4 5

LEVEL 42: Pyramid with Numbers Mirrored

C
#include <stdio.h>
int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Spaces
for(int j = 1; j <= n-i; j++)
printf(" ");
// Left half
for(int j = i; j >= 1; j--)
printf("%d ", j);
// Right half
for(int j = 2; j <= i; j++)
printf("%d ", j);
printf("\n");
}

return 0;
}

Output:

text
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5

LEVEL 43: Alternating Character Pyramid

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++) {
if(j % 2 == 1)
printf("*");
else
printf("#");
}
printf("\n");
}

return 0;
}

Output:

text
*
*#*
*#*#*
*#*#*#*
*#*#*#*#*

LEVEL 44: Continuous Alphabet Pattern

C
#include <stdio.h>

int main() {
int n = 5;
char ch = 'A';

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
printf("%c ", ch);
ch++;
}
printf("\n");
}

return 0;
}

Output:

text
A
B C
D E F
G H I J
K L M N O
LEVEL 45: Reverse Alphabet Triangle

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
printf("%c ", 'A' + n - i);
}
printf("\n");
}

return 0;
}

Output:

text
E
D D
C C C
B B B B
A A A A A

LEVEL 46: Hollow Rhombus

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= n; j++) {
if(i == 1 || i == n || j == 1 || j == n)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
*****
* *
* *
* *
*****

LEVEL 47: Box with Diagonals

C
#include <stdio.h>

int main() {
int n = 7; // Should be odd for symmetry
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i == 1 || i == n || j == 1 || j == n ||
i == j || i + j == n + 1)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
*******
** **
* * * *
* * *
* * * *
** **
*******

LEVEL 48: Number Hourglass

C
#include <stdio.h>

int main() {
int n = 5;

// Upper part
for(int i = 1; i <= n; i++) {
for(int j = 1; j < i; j++)
printf(" ");
for(int j = i; j <= n; j++)
printf("%d ", j);
printf("\n");
}

// Lower part
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j < i; j++)
printf(" ");
for(int j = i; j <= n; j++)
printf("%d ", j);
printf("\n");
}

return 0;
}

Output:

text
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
LEVEL 49: Crown Pattern

C
#include <stdio.h>

int main() {
int n = 5;

// Top peaks
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= 2*n+1; j++) {
if((j == 1 && i >= 1) || (j == n+1 && i >= 1) ||
(j == 2*n+1 && i >= 1)) {
if(i <= (n+1)/2 || i > n - (n+1)/2 ||
(j >= n - i + 2 && j <= n + i))
printf("*");
else
printf(" ");
}
else if(i == n)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 50: Hollow Diamond in Rectangle

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= 2*n-1; i++) {


for(int j = 1; j <= 2*n-1; j++) {
int dist = 0;

if(i <= n)
dist = n - i;
else
dist = i - n;

if(j <= dist || j >= 2*n - dist)


printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
*********
**** ***
*** **
** *
*
** *
*** **
**** ***
*********

🌟 PART 5: LEVEL 51-75 (Advanced Patterns)

LEVEL 51: Star of David

C
#include <stdio.h>

int main() {
int n = 5;

// Upper triangle
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*n+2*i-1; j++) {
if(j == 1 || j == 2*i-1 || (i == n))
printf("*");
else
printf(" ");
}
printf("\n");
}

// Lower inverted triangle


for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++) {
if(j == 1 || j == 2*i-1)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 52: Numeric Hollow Pyramid


C
#include <stdio.h>

int main() {
int n = 5;
int num = 1;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n-i; j++)
printf(" ");

for(int j = 1; j <= 2*i-1; j++) {


if(j == 1)
printf("%d", i);
else if(j == 2*i-1)
printf("%d", i);
else if(i == n)
printf("%d", ((j+1)/2));
else
printf(" ");
}
printf("\n");
}
return 0;
}

Output:

text
1
2 2
3 3
4 4
123454321

LEVEL 53: Pascal Triangle with Binomial

C
#include <stdio.h>

long factorial(int n) {
long fact = 1;
for(int i = 2; i <= n; i++)
fact *= i;
return fact;
}

long nCr(int n, int r) {


return factorial(n) / (factorial(r) * factorial(n-r));
}

int main() {
int n = 7;

for(int i = 0; i < n; i++) {


for(int j = 0; j < n-i-1; j++)
printf(" ");

for(int j = 0; j <= i; j++)


printf("%6ld", nCr(i, j));

printf("\n");
}

return 0;
}

Output:
text
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

LEVEL 54: Fibonacci Triangle

C
#include <stdio.h>

int main() {
int n = 5;
int a = 0, b = 1, c;

for(int i = 1; i <= n; i++) {


a = 0; b = 1;
for(int j = 1; j <= i; j++) {
if(j == 1)
printf("0 ");
else if(j == 2)
printf("1 ");
else {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
}
printf("\n");
}

return 0;
}

Output:

text
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3

LEVEL 55: Prime Number Triangle

C
#include <stdio.h>

int isPrime(int n) {
if(n < 2) return 0;
for(int i = 2; i * i <= n; i++)
if(n % i == 0) return 0;
return 1;
}

int main() {
int n = 5;
int num = 2;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
while(!isPrime(num)) num++;
printf("%d ", num);
num++;
}
printf("\n");
}

return 0;
}

Output:

text
2
3 5
7 11 13
17 19 23 29
31 37 41 43 47

LEVEL 56: Magic Square Pattern

C
#include <stdio.h>
int main() {
int n = 5; // Must be odd
int magic[10][10] = {0};
int num = 1;
int i = 0, j = n/2;

while(num <= n*n) {


magic[i][j] = num;
num++;

int newi = (i - 1 + n) % n;
int newj = (j + 1) % n;

if(magic[newi][newj])
i = (i + 1) % n;
else {
i = newi;
j = newj;
}
}

for(i = 0; i < n; i++) {


for(j = 0; j < n; j++)
printf("%3d", magic[i][j]);
printf("\n");
}

return 0;
}

Output:

text
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

LEVEL 57: Sieve of Eratosthenes Visual

C
#include <stdio.h>

int main() {
int n = 50;
int sieve[51];
int cols = 10;

for(int i = 0; i <= n; i++) sieve[i] = 1;


sieve[0] = sieve[1] = 0;

for(int i = 2; i*i <= n; i++)


if(sieve[i])
for(int j = i*i; j <= n; j += i)
sieve[j] = 0;

for(int i = 1; i <= n; i++) {


if(sieve[i])
printf("%3d", i);
else
printf(" -");
if(i % cols == 0) printf("\n");
}

return 0;
}
LEVEL 58: Checkerboard Pattern

C
#include <stdio.h>

int main() {
int n = 8;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n; j++) {
if((i + j) % 2 == 0)
printf("█ ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 59: Snake Pattern Numbers


C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


if(i % 2 == 1) {
// Left to right
for(int j = 1; j <= n; j++)
printf("%3d", (i-1)*n + j);
} else {
// Right to left
for(int j = n; j >= 1; j--)
printf("%3d", (i-1)*n + j);
}
printf("\n");
}

return 0;
}

Output:
text
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25

LEVEL 60: Wave Pattern

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n; j++) {
if(j % 2 == 1) {
// Odd columns: top to bottom
printf("%3d", (j-1)*n + i);
} else {
// Even columns: bottom to top
printf("%3d", j*n - i + 1);
}
}
printf("\n");
}

return 0;
}

Output:

text
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25

LEVEL 61: Hourglass with Numbers

C
#include <stdio.h>
int main() {
int n = 5;

// Upper half
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++)
printf(" ");
for(int j = i; j < n; j++)
printf("%d ", n-j);
printf("\n");
}

// Lower half
for(int i = n-2; i >= 0; i--) {
for(int j = 0; j < i; j++)
printf(" ");
for(int j = i; j < n; j++)
printf("%d ", n-j);
printf("\n");
}

return 0;
}
Output:

text
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

LEVEL 62: Number Butterfly

C
#include <stdio.h>

int main() {
int n = 4;
// Upper half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++)
printf("%d", j);
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
for(int j = i; j >= 1; j--)
printf("%d", j);
printf("\n");
}

// Lower half
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= i; j++)
printf("%d", j);
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
for(int j = i; j >= 1; j--)
printf("%d", j);
printf("\n");
}

return 0;
}
Output:

text
1 1
12 21
123 321
12344321
12344321
123 321
12 21
1 1

LEVEL 63: Alpha-Numeric Triangle

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= i; j++) {
if(j % 2 == 1)
printf("%d ", j);
else
printf("%c ", 'A' + j - 1);
}
printf("\n");
}

return 0;
}

Output:

text
1
1 B
1 B 3
1 B 3 D
1 B 3 D 5

LEVEL 64: Cross Pattern with Numbers


C
#include <stdio.h>

int main() {
int n = 7; // Should be odd
int mid = n / 2;

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
if(i == mid)
printf("%d ", j + 1);
else if(j == mid)
printf("%d ", i + 1);
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:
text
1
2
3
1 2 3 4 5 6 7
5
6
7

LEVEL 65: Alphabet Hollow Diamond

C
#include <stdio.h>

int main() {
int n = 5;

// Upper half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++) {
if(j == 1 || j == 2*i-1)
printf("%c", 'A' + i - 1);
else
printf(" ");
}
printf("\n");
}

// Lower half
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= 2*i-1; j++) {
if(j == 1 || j == 2*i-1)
printf("%c", 'A' + i - 1);
else
printf(" ");
}
printf("\n");
}

return 0;
}
Output:

text
A
B B
C C
D D
E E
D D
C C
B B
A

LEVEL 66: Dual Pyramid

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


// Left pyramid
for(int j = 1; j <= i; j++)
printf("*");
// Middle spaces
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
// Right pyramid
for(int j = 1; j <= i; j++)
printf("*");
printf("\n");
}

for(int i = n-1; i >= 1; i--) {


for(int j = 1; j <= i; j++)
printf("*");
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
for(int j = 1; j <= i; j++)
printf("*");
printf("\n");
}

return 0;
}
Output:

text
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *

LEVEL 67: Number Hollow Square

C
#include <stdio.h>

int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i == 1 || i == n || j == 1 || j == n)
printf("%d ", i * j);
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
1 2 3 4 5
2 10
3 15
4 20
5 10 15 20 25
LEVEL 68: Spiral Matrix (Anti-clockwise)

C
#include <stdio.h>

int main() {
int n = 5;
int a[10][10];
int num = 1;
int layer = 0;

while(num <= n*n) {


// Left column
for(int i = layer; i < n-layer && num <= n*n; i++)
a[i][layer] = num++;

// Bottom row
for(int j = layer+1; j < n-layer && num <= n*n; j++)
a[n-layer-1][j] = num++;

// Right column
for(int i = n-layer-2; i >= layer && num <= n*n; i--)
a[i][n-layer-1] = num++;
// Top row
for(int j = n-layer-2; j > layer && num <= n*n; j--)
a[layer][j] = num++;

layer++;
}

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++)
printf("%3d", a[i][j]);
printf("\n");
}

return 0;
}

LEVEL 69: Number Circle Pattern

C
#include <stdio.h>
#include <math.h>
int main() {
int n = 11; // Should be odd
int r = n / 2;

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
double dist = sqrt((i-r)*(i-r) + (j-r)*(j-r));
if(dist >= r-0.5 && dist <= r+0.5)
printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 70: Olympic Rings Pattern


C
#include <stdio.h>
#include <math.h>

int inRing(int x, int y, int cx, int cy, int r) {


double dist = sqrt((x-cx)*(x-cx) + (y-cy)*(y-cy));
return (dist >= r-0.5 && dist <= r+0.5);
}

int main() {
int h = 9, w = 23;
int r = 3;

for(int i = 0; i < h; i++) {


for(int j = 0; j < w; j++) {
if(inRing(i, j, 3, 4, r) ||
inRing(i, j, 3, 11, r) ||
inRing(i, j, 3, 18, r) ||
inRing(i, j, 5, 7, r) ||
inRing(i, j, 5, 15, r))
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 71: Binary Tree Pattern

C
#include <stdio.h>
#include <math.h>

int main() {
int levels = 4;
int width = (1 << levels) - 1; // 2^levels - 1

for(int i = 0; i < levels; i++) {


int nodes = 1 << i; // 2^i nodes at level i
int gap = width / (nodes + 1);

for(int j = 0; j < nodes; j++) {


for(int k = 0; k < (j == 0 ? gap : 2*gap); k++)
printf(" ");
printf("*");
}
printf("\n");
}

return 0;
}

LEVEL 72: Arrow Head Pattern

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= i; j++)
printf("* ");
printf("\n");
}

for(int i = 1; i <= n; i++) {


for(int j = 1; j <= n-1; j++)
printf(" ");
printf("* \n");
}

return 0;
}

Output:

text
*
* *
* * *
* * * *
* * * * *
*
*
*
*
*

LEVEL 73: Christmas Tree Pattern

C
#include <stdio.h>

int main() {
int n = 4; // Number of sections
int maxWidth = 2 * n + 2 * n - 1;

for(int section = 1; section <= n; section++) {


for(int i = 1; i <= section + 2; i++) {
int stars = 2 * i - 1;
int spaces = (maxWidth - stars) / 2;

for(int j = 0; j < spaces; j++)


printf(" ");
for(int j = 0; j < stars; j++)
printf("*");
printf("\n");
}
}

// Trunk
int trunkWidth = 3;
int trunkHeight = 2;
for(int i = 0; i < trunkHeight; i++) {
for(int j = 0; j < (maxWidth - trunkWidth) / 2; j++)
printf(" ");
for(int j = 0; j < trunkWidth; j++)
printf("*");
printf("\n");
}

return 0;
}

LEVEL 74: Matrix Rotation Pattern

C
#include <stdio.h>
int main() {
int n = 4;
int a[10][10], b[10][10];
int num = 1;

// Initialize
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
a[i][j] = num++;

printf("Original:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
printf("%3d", a[i][j]);
printf("\n");
}

// Rotate 90 degrees clockwise


for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
b[j][n-1-i] = a[i][j];

printf("\nRotated 90°:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
printf("%3d", b[i][j]);
printf("\n");
}

return 0;
}

LEVEL 75: Matrix Border Rotation

C
#include <stdio.h>

int main() {
int n = 5;
int a[10][10];
int num = 1;

// Initialize
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
a[i][j] = num++;

// Store border
int border[100], idx = 0;

// Top row
for(int j = 0; j < n; j++) border[idx++] = a[0][j];
// Right column
for(int i = 1; i < n; i++) border[idx++] = a[i][n-1];
// Bottom row
for(int j = n-2; j >= 0; j--) border[idx++] = a[n-1][j];
// Left column
for(int i = n-2; i > 0; i--) border[idx++] = a[i][0];

// Rotate border array


int temp = border[0];
for(int i = 0; i < idx-1; i++)
border[i] = border[i+1];
border[idx-1] = temp;

// Put back
idx = 0;
for(int j = 0; j < n; j++) a[0][j] = border[idx++];
for(int i = 1; i < n; i++) a[i][n-1] = border[idx++];
for(int j = n-2; j >= 0; j--) a[n-1][j] = border[idx++];
for(int i = n-2; i > 0; i--) a[i][0] = border[idx++];

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++)
printf("%3d", a[i][j]);
printf("\n");
}

return 0;
}

🔥 PART 6: LEVEL 76-100 (HARDCORE PATTERNS)

LEVEL 76: Fractal Triangle (Sierpinski)

C
#include <stdio.h>

int main() {
int n = 16; // Should be power of 2
for(int y = n-1; y >= 0; y--) {
for(int x = 0; x < y; x++)
printf(" ");
for(int x = 0; x + y < n; x++) {
if((x & y) != 0)
printf(" ");
else
printf("* ");
}
printf("\n");
}

return 0;
}

Output:

text
*
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *

LEVEL 77: Number Spiral (Ulam Spiral)

C
#include <stdio.h>
#include <math.h>

int isPrime(int n) {
if(n < 2) return 0;
for(int i = 2; i <= sqrt(n); i++)
if(n % i == 0) return 0;
return 1;
}

int main() {
int n = 9;
int a[15][15] = {0};
int x = n/2, y = n/2;
int num = 1;
int dir = 0; // 0:right, 1:up, 2:left, 3:down
int steps = 1;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

while(num <= n*n) {


for(int twice = 0; twice < 2 && num <= n*n; twice++) {
for(int i = 0; i < steps && num <= n*n; i++) {
a[y][x] = num++;
x += dx[dir];
y += dy[dir];
}
dir = (dir + 1) % 4;
}
steps++;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(isPrime(a[i][j]))
printf("[%2d]", a[i][j]);
else
printf(" %2d ", a[i][j]);
}
printf("\n");
}

return 0;
}

LEVEL 78: Complex Hollow Diamond

C
#include <stdio.h>

int main() {
int n = 9; // Should be odd
int mid = n / 2;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
int di = (i <= mid) ? i : n - 1 - i;
int dj = (j <= mid) ? j : n - 1 - j;
int dist = (di < dj) ? di : dj;

if(dist == 0 || dist == mid)


printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 79: 3D Cube Pattern

C
#include <stdio.h>
int main() {
int n = 5;

// Top face
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - i; j++)
printf(" ");
for(int j = 0; j < n; j++)
printf("/ ");
printf("\n");
}

// Side faces
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++)
printf(" ");
printf("/ ");
for(int j = 0; j < n - 1; j++)
printf(" ");
printf("/ ");
for(int j = 0; j < n - 1; j++)
printf(" ");
printf("/\n");
}

return 0;
}

LEVEL 80: Pascal + Fibonacci Hybrid

C
#include <stdio.h>

int main() {
int n = 7;
int fib[20] = {0, 1};

for(int i = 2; i < 20; i++)


fib[i] = fib[i-1] + fib[i-2];

for(int i = 0; i < n; i++) {


for(int j = 0; j < n - i - 1; j++)
printf(" ");

for(int j = 0; j <= i; j++) {


if(j == 0 || j == i)
printf("%3d", fib[i]);
else
printf("%3d", fib[i-1] + fib[j]);
}
printf("\n");
}

return 0;
}

LEVEL 81: Hollow Star of David

C
#include <stdio.h>

int main() {
int n = 7;
int width = 2*n - 1;

// Upper triangle
for(int i = 0; i < n; i++) {
for(int j = 0; j < width; j++) {
int pos = j - (n - 1 - i);
if(pos >= 0 && pos <= 2*i) {
if(pos == 0 || pos == 2*i || i == n-1)
printf("*");
else
printf(" ");
} else {
printf(" ");
}
}
printf("\n");
}

// Lower inverted triangle


for(int i = n-2; i >= 0; i--) {
for(int j = 0; j < width; j++) {
int pos = j - (n - 1 - i);
if(pos >= 0 && pos <= 2*i) {
if(pos == 0 || pos == 2*i || i == n-2)
printf("*");
else
printf(" ");
} else {
printf(" ");
}
}
printf("\n");
}

return 0;
}

LEVEL 82: Nested Squares Pattern

C
#include <stdio.h>

int main() {
int n = 9; // Should be odd

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
int minDist = i;
if(j < minDist) minDist = j;
if(n-1-i < minDist) minDist = n-1-i;
if(n-1-j < minDist) minDist = n-1-j;

if(minDist % 2 == 0)
printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
* * * * * * * * *
* *
* * * * * * * * *
* * * *
* * * * * * *
* * * *
* * * * * * * * *
* *
* * * * * * * * *

LEVEL 83: Concentric Diamond Layers

C
#include <stdio.h>
#include <stdlib.h>

int main() {
int n = 9;
int mid = n / 2;

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
int di = abs(i - mid);
int dj = abs(j - mid);
int dist = di + dj;

if(dist <= mid && dist % 2 == 0)


printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 84: Number Pyramid with Sum

C
#include <stdio.h>

int main() {
int n = 5;

for(int i = 1; i <= n; i++) {


int sum = 0;

// Leading spaces
for(int j = 1; j <= n-i; j++)
printf(" ");
// Numbers
for(int j = 1; j <= i; j++) {
printf("%3d", j);
sum += j;
}
for(int j = i-1; j >= 1; j--) {
printf("%3d", j);
sum += j;
}

printf(" = %d\n", sum);


}

return 0;
}

Output:

text
1 = 1
1 2 1 = 4
1 2 3 2 1 = 9
1 2 3 4 3 2 1 = 16
1 2 3 4 5 4 3 2 1 = 25

LEVEL 85: Roman Numeral Triangle

C
#include <stdio.h>

void printRoman(int n) {
int values[] = {10, 9, 5, 4, 1};
char *symbols[] = {"X", "IX", "V", "IV", "I"};

for(int i = 0; i < 5; i++) {


while(n >= values[i]) {
printf("%s", symbols[i]);
n -= values[i];
}
}
}

int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
printRoman(j);
printf(" ");
}
printf("\n");
}

return 0;
}

Output:

text
I
I II
I II III
I II III IV
I II III IV V

LEVEL 86: Binary Pattern (Powers of 2)


C
#include <stdio.h>

void printBinary(int n, int bits) {


for(int i = bits-1; i >= 0; i--) {
printf("%d", (n >> i) & 1);
}
}

int main() {
int n = 5;

for(int i = 0; i < n; i++) {


for(int j = 0; j <= i; j++) {
printBinary(j, n);
printf(" ");
}
printf("\n");
}

return 0;
}
Output:

text
00000
00000 00001
00000 00001 00010
00000 00001 00010 00011
00000 00001 00010 00011 00100

LEVEL 87: Hexagonal Pattern

C
#include <stdio.h>

int main() {
int n = 4;

// Upper half
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - 1 - i; j++)
printf(" ");
for(int j = 0; j < n + i; j++)
printf("* ");
printf("\n");
}

// Lower half
for(int i = n - 2; i >= 0; i--) {
for(int j = 0; j < n - 1 - i; j++)
printf(" ");
for(int j = 0; j < n + i; j++)
printf("* ");
printf("\n");
}

return 0;
}

Output:

text
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *

LEVEL 88: Spiral Pattern with Characters

C
#include <stdio.h>

int main() {
int n = 5;
char a[10][10];
char ch = 'A';
int top = 0, bottom = n-1, left = 0, right = n-1;

while(top <= bottom && left <= right) {


for(int i = left; i <= right; i++)
a[top][i] = ch++;
top++;

for(int i = top; i <= bottom; i++)


a[i][right] = ch++;
right--;

if(top <= bottom) {


for(int i = right; i >= left; i--)
a[bottom][i] = ch++;
bottom--;
}

if(left <= right) {


for(int i = bottom; i >= top; i--)
a[i][left] = ch++;
left++;
}
}

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++)
printf("%c ", a[i][j]);
printf("\n");
}

return 0;
}
Output:

text
A B C D E
P Q R S F
O X Y T G
N W V U H
M L K J I

LEVEL 89: Complex Wave Pattern

C
#include <stdio.h>
#include <math.h>

int main() {
int width = 60;
int height = 15;
double amplitude = 5.0;

for(int y = 0; y < height; y++) {


for(int x = 0; x < width; x++) {
double wave = amplitude * sin(x * 0.2) + height/2;
if((int)wave == y)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 90: Maze Pattern

C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
int n = 11; // Should be odd
int maze[15][15];
srand(time(0));

// Initialize with walls


for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
maze[i][j] = 1;

// Simple maze generation


for(int i = 1; i < n-1; i += 2) {
for(int j = 1; j < n-1; j += 2) {
maze[i][j] = 0;
if(rand() % 2 && j+1 < n-1)
maze[i][j+1] = 0;
if(rand() % 2 && i+1 < n-1)
maze[i+1][j] = 0;
}
}

// Print maze
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(maze[i][j])
printf("██");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 91: Multiplication Pattern Matrix

C
#include <stdio.h>

int main() {
int n = 9;

printf(" |");
for(int j = 1; j <= n; j++)
printf("%4d", j);
printf("\n---+");
for(int j = 1; j <= n; j++)
printf("----");
printf("\n");

for(int i = 1; i <= n; i++) {


printf("%2d |", i);
for(int j = 1; j <= n; j++) {
printf("%4d", i * j);
}
printf("\n");
}

return 0;
}

LEVEL 92: Star with Central Hollow

C
#include <stdio.h>
#include <stdlib.h>

int main() {
int n = 11; // Should be odd
int mid = n / 2;
int hollow = 2; // Hollow radius

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
int di = abs(i - mid);
int dj = abs(j - mid);

// Star points or cross


if(i == mid || j == mid || di == dj) {
if(di + dj <= hollow)
printf(" ");
else
printf("* ");
} else {
printf(" ");
}
}
printf("\n");
}

return 0;
}
LEVEL 93: Number Avalanche

C
#include <stdio.h>

int main() {
int n = 5;
int num = 1;

// Upper part
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= i; j++) {
printf("%3d", num++);
}
printf("\n");
}

// Lower part
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= n-i; j++)
printf(" ");
for(int j = 1; j <= i; j++) {
printf("%3d", num++);
}
printf("\n");
}

return 0;
}

Output:

text
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19
20 21 22
23 24
25
LEVEL 94: Catalan Triangle

C
#include <stdio.h>

long catalan(int n, int k) {


if(k < 0 || k > n) return 0;
if(n == 0) return 1;
if(k == 0) return catalan(n-1, 0);
return catalan(n, k-1) + catalan(n-1, k);
}

int main() {
int n = 6;

for(int i = 0; i < n; i++) {


for(int j = 0; j < n - i; j++)
printf(" ");
for(int j = 0; j <= i; j++)
printf("%5ld", catalan(i, j));
printf("\n");
}
return 0;
}

LEVEL 95: Matrix Diagonal Patterns

C
#include <stdio.h>

int main() {
int n = 5;

// Main diagonal and anti-diagonal values


for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i == j)
printf("%2d ", i + j + 1);
else if(i + j == n - 1)
printf("%2d ", n);
else
printf(" . ");
}
printf("\n");
}

return 0;
}

Output:

text
1 . . . 5
. 3 . 5 .
. . 5 . .
. 7 . 7 .
9 . . . 9

LEVEL 96: Complex Butterfly with Numbers

C
#include <stdio.h>

int main() {
int n = 5;
// Upper half
for(int i = 1; i <= n; i++) {
// Left wing
for(int j = 1; j <= i; j++)
printf("%d", j);
// Gap
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
// Right wing
for(int j = i; j >= 1; j--)
printf("%d", j);
printf("\n");
}

// Lower half
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= i; j++)
printf("%d", j);
for(int j = 1; j <= 2*(n-i); j++)
printf(" ");
for(int j = i; j >= 1; j--)
printf("%d", j);
printf("\n");
}
return 0;
}

Output:

text
1 1
12 21
123 321
1234 4321
1234554321
1234 4321
123 321
12 21
1 1

LEVEL 97: Hollow Hexagon

C
#include <stdio.h>
int main() {
int n = 5;

// Upper part
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - 1 - i; j++)
printf(" ");
printf("*");
for(int j = 0; j < n + 2*i - 1; j++) {
if(i == 0)
printf("*");
else
printf(" ");
}
if(i > 0)
printf("*");
printf("\n");
}

// Lower part
for(int i = n - 2; i >= 0; i--) {
for(int j = 0; j < n - 1 - i; j++)
printf(" ");
printf("*");
for(int j = 0; j < n + 2*i - 1; j++) {
if(i == 0)
printf("*");
else
printf(" ");
}
if(i > 0)
printf("*");
printf("\n");
}

return 0;
}

LEVEL 98: Number Vortex

C
#include <stdio.h>

int main() {
int n = 7;
int a[15][15];
int layers = (n + 1) / 2;

for(int layer = 0; layer < layers; layer++) {


int val = layer + 1;
for(int i = layer; i < n - layer; i++) {
for(int j = layer; j < n - layer; j++) {
a[i][j] = val;
}
}
}

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++)
printf("%2d ", a[i][j]);
printf("\n");
}

return 0;
}

Output:
text
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1

LEVEL 99: Ultimate Star Pattern

C
#include <stdio.h>
#include <stdlib.h>

int main() {
int n = 15; // Should be odd
int mid = n / 2;
int quarter = n / 4;

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
int di = abs(i - mid);
int dj = abs(j - mid);

// Star shape
int cond1 = (i == mid); // Horizontal line
int cond2 = (j == mid); // Vertical line
int cond3 = (di == dj && di <= quarter); // Small X
int cond4 = (di + dj == mid); // Diamond edges

if(cond1 || cond2 || cond3 || cond4)


printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

LEVEL 100: THE ULTIMATE CHALLENGE - Complete


Mandala Pattern 🏆
C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {
int n = 21; // Should be odd
int mid = n / 2;

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
int di = abs(i - mid);
int dj = abs(j - mid);
double dist = sqrt(di*di + dj*dj);

// Multiple concentric circles


int circle1 = (dist >= mid-0.5 && dist <= mid+0.5);
int circle2 = (dist >= mid/2-0.5 && dist <= mid/2+0.5);
int circle3 = (dist >= mid/4-0.5 && dist <= mid/4+0.5);

// Cross pattern
int cross = (i == mid || j == mid);
// Diagonal pattern
int diag = (di == dj);

// Diamond
int diamond = (di + dj == mid);

// Inner square
int square = (di == mid/3 || dj == mid/3) &&
(di <= mid/3 && dj <= mid/3);

if(circle1 || circle2 || cross || diag || diamond || square)


printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}

Output (Simplified representation):


text
*
* * * * *
* * * * *
* * *
* * * * *
* * *
* * * * * * * * *
* * *
* * * * *
* * *
* * * * * * * * * * *
* * *
* * * * *
* * *
* * * * * * * * *
* * *
* * * * *
* * *
* * * * *
* * * * *
*
Something went wrong with this response, please try again.

You might also like