Table of Contents
1. Solid Square
Python
#Expected Output for n = 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *| R\C | j=1 | j=2 | j=3 | j=4 | j=5 |
|---|---|---|---|---|---|
| i=1 | * | * | * | * | * |
| i=2 | * | * | * | * | * |
| i=3 | * | * | * | * | * |
| i=4 | * | * | * | * | * |
| i=5 | * | * | * | * | * |
- Both i and j loop should run for n times
Python
n = int(input())
for i in range(n):
for j in range(n):
print("*", end=" ")
print()Python
#String Multiplication
n = int(input())
for i in range(n):
print("* " * n)2. Right-Angled Triangle
Python
#Expected Output for n = 5
*
* *
* * *
* * * *
* * * * *Python
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5Python
A
A B
A B C
A B C D
A B C D E| R\C | j=1 | j=2 | j=3 | j=4 | j=5 |
|---|---|---|---|---|---|
| i=1 | * | ||||
| i=2 | * | * | |||
| i=3 | * | * | * | ||
| i=4 | * | * | * | * | |
| i=5 | * | * | * | * | * |
* should be printed until j<=i
Python
n=int(input())
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()Python
#String multiplication
n=int(input())
for i in range(1, n+1):
print("* " * i)Python
n=int(input())
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end=" ")
print()Python
n=int(input())
for i in range(1, n+1):
for j in range(i):
print(chr(65+j), end=" ")
print()3. Inverted Right-Angled Triangle
Python
* * * * *
* * * *
* * *
* *
*Python
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1Python
A B C D E
A B C D
A B C
A B
A| R\C | j=1 | j=2 | j=3 | j=4 | j=5 |
|---|---|---|---|---|---|
| i=1 | * | * | * | * | * |
| i=2 | * | * | * | * | |
| i=3 | * | * | * | ||
| i=4 | * | * | |||
| i=5 | * |
- Similar to Right-Angled Triangle. Just start the loop from n
Python
n=int(input())
for i in range(n, 0, -1):
for j in range(i):
print("*", end=" ")
print()Python
n=int(input())
for i in range(n, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()Python
n=int(input())
for i in range(n, 0, -1):
for j in range(i):
print(chr(65+j), end=" ")
print()4. Right-Aligned Triangle
Python
#Expected Output for n = 5
*
**
***
****
*****| R\C | j=1 | j=2 | j=3 | j=4 | j=5 |
|---|---|---|---|---|---|
| i=1 | * | ||||
| i=2 | * | * | |||
| i=3 | * | * | * | ||
| i=4 | * | * | * | * | |
| i=5 | * | * | * | * | * |
- no. of white spaces: n-i and no. of '*' : i
Python
n=int(input())
for i in range(1,n+1):
for j in range(n-i): #Loop runs for n-i times, since we have n-i white spaces
print(' ',end='')
for k in range(i): #Loop runs for i times, since we have i '*'
print('*',end='')
print()Python
n=int(input())
for i in range(1,n+1):
print(' '*(n-i)+'*'*i) #String concatenation5. Inverted Right-Aligned Triangle
Python
*****
****
***
**
*| R\C | j=1 | j=2 | j=3 | j=4 | j=5 |
|---|---|---|---|---|---|
| i=1 | * | * | * | * | * |
| i=2 | * | * | * | * | |
| i=3 | * | * | * | ||
| i=4 | * | * | |||
| i=5 | * |
- Similar to Right Aligned Triangle. Just start the loop from n
Python
n = int(input())
for i in range(n, 0, -1):
print(" " * (n - i) + "*" * i)6. Full Pyramid
Python
*
***
*****
*******
*********Python
*
* *
* * *
* * * *
* * * * *| i=1 | * | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| i=2 | * | * | * | ||||||
| i=3 | * | * | * | * | * | ||||
| i=4 | * | * | * | * | * | * | * | ||
| i=5 | * | * | * | * | * | * | * | * | * |
- 2*i-1 '*' and n-i spaces
| i=1 | * | W | |||||||
|---|---|---|---|---|---|---|---|---|---|
| i=2 | * | W | * | W | |||||
| i=3 | * | W | * | W | * | W | |||
| i=4 | * | W | * | W | * | W | * | W | |
| i=5 | * | W | * | W | * | W | * | W | * |
Here W is the white space
- This is similar to Right-Aligned Triangle. Just add a space after * i.e. '* '
Python
n = int(input())
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))Python
n = int(input())
for i in range(1,n+1):
print(' '*(n-i)+'* '*i)7. Inverted Pyramid
Python
* * * * *
* * * *
* * *
* *
*Python
*********
*******
*****
***
*- It is similar to Full Pyramid. Just start the loop from n
Python
n = int(input())
for i in range(n,0,-1):
print(' '*(n-i)+'* '*i)Python
n = int(input())
for i in range(n,0,-1):
print(" " * (n - i) + "*" * (2 * i - 1))8. Diamond
Python
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*- Upper-half is Full Pyramid and lower-half is Inverted Pyramid but loop starts from n-1
Python
n = int(input())
for i in range(1,n+1):
print(' '*(n-i)+'* '*i)
for i in range(n-1,0,-1):
print(' '*(n-i)+'* '*i)9. Flyod's Triangle
Python
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15| R\C | j=1 | j=2 | j=3 | j=4 | j=5 |
|---|---|---|---|---|---|
| i=1 | 1 | ||||
| i=2 | 2 | 3 | |||
| i=3 | 4 | 5 | 6 | ||
| i=4 | 7 | 8 | 9 | 10 | |
| i=5 | 11 | 12 | 13 | 14 | 15 |
- Similar to Right-Angled Triangle. Here we take num and increment it
Python
n = int(input())
num = 1
for i in range(n+1):
for j in range(i):
print(num, end=' ')
num += 1
print()10. Pascal's Triangle
Python
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1| R\C | j=1 | j=2 | j=3 | j=4 | j=5 | j=6 |
|---|---|---|---|---|---|---|
| i=1 | 1 | |||||
| i=2 | 1 | 1 | ||||
| i=3 | 1 | 2 | 1 | |||
| i=4 | 1 | 3 | 3 | 1 | ||
| i=5 | 1 | 4 | 6 | 4 | 1 | |
| i=6 | 1 | 5 | 10 | 10 | 5 | 1 |
- Pascal's Triangle formula: val = val * (i - j) // (j + 1)
- n-i spaces and i no. of numbers
Python
n = int(input())
for i in range(n):
print(' '*(n-i),end='')
val = 1
for j in range(i+1):
print(val, end=' ')
val = val * (i - j) // (j + 1)
print()11. Binary Triangle
Python
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1| R\C | j=1 | j=2 | j=3 | j=4 | j=5 |
|---|---|---|---|---|---|
| i=1 | 1 | ||||
| i=2 | 0 | 1 | |||
| i=3 | 1 | 0 | 1 | ||
| i=4 | 0 | 1 | 0 | 1 | |
| i=5 | 1 | 0 | 1 | 0 | 1 |
- print 1 if i + j = even else 0
Python
n = int(input())
for i in range(1,n+1):
for j in range(1,i+1):
if (i+j) % 2 == 0:
print(1, end=' ')
else:
print(0, end=' ')
print()12. Palindrome Number
Python
1
121
12321
1234321
123454321| i=1 | 1 | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| i=2 | 1 | 2 | 1 | ||||||
| i=3 | 1 | 2 | 3 | 2 | 1 | ||||
| i=4 | 1 | 2 | 3 | 4 | 3 | 2 | 1 | ||
| i=5 | 1 | 2 | 3 | 4 | 5 | 4 | 3 | 2 | 1 |
- n-i spaces
- Split the pattern i.e. jth loop to print ascending order and kth loop to print descending order but starting from i-1
Python
n = int(input())
for i in range(1,n+1):
print(' '*(n-i),end='')
for j in range(1,i+1):
print(j, end='')
for k in range(i-1,0,-1):
print(k, end='')
print()13. Hollow Square
Python
* * * * *
* *
* *
* *
* * * * *| R\C | j=1 | j=2 | j=3 | j=4 | j=5 |
|---|---|---|---|---|---|
| i=1 | * | * | * | * | * |
| i=2 | * | * | |||
| i=3 | * | * | |||
| i=4 | * | * | |||
| i=5 | * | * | * | * | * |
- '*' is being printed at i = 1 , n and j = 1 , n else whitespace
Python
n = int(input())
for i in range(1,n+1):
for j in range(1,n+1):
if i == 1 or i == n or j == 1 or j == n:
print('*', end=' ')
else:
print(' ',end=' ')
print()14. Hollow Pyramid
Python
*
* *
* *
* *
*********| i=1 | * | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| i=2 | * | * | |||||||
| i=3 | * | * | |||||||
| i=4 | * | * | |||||||
| i=5 | * | * | * | * | * | * | * | * | * |
- n-i spaces and jth loop for 1 to 2*i times
- We need to print * at the ends i.e. j = 1 or j = 2 * i -1 and i = n else whitespace
Python
n = int(input())
for i in range(1, n + 1):
print(" " * (n - i), end="")
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1 or i == n:
print("*", end="")
else:
print(" ", end="")
print()15. Butterfly
Python
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *| i=1 | * | * | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| i=2 | * | * | * | * | ||||||
| i=3 | * | * | * | * | * | * | ||||
| i=4 | * | * | * | * | * | * | * | * | ||
| i=5 | * | * | * | * | * | * | * | * | * | * |
| i=4 | * | * | * | * | * | * | * | * | ||
| i=3 | * | * | * | * | * | * | ||||
| i=2 | * | * | * | * | ||||||
| i=1 | * | * |
- Split into upper and lower halves. 2i no. of stars and 2 * (n-i) whitespaces
Python
n = int(input())
for i in range(1, n + 1):
print("*" * i + " " * (2 * (n - i)) + "*" * i)
for i in range(n-1, 0, -1):
print("*" * i + " " * (2 * (n - i)) + "*" * i)16. Cross
Python
* *
* *
*
* *
* *| R\C | j=1 | j=2 | j=3 | j=4 | j=5 |
|---|---|---|---|---|---|
| i=1 | * | * | |||
| i=2 | * | * | |||
| i=3 | * | ||||
| i=4 | * | * | |||
| i=5 | * | * |
- Print * if i==j or i+j == n-1
Python
n = int(input())
for i in range(n):
for j in range(n):
if i == j or i + j == n - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()17. Hourglass
Python
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *| i=1 | * | * | * | * | * |
|---|---|---|---|---|---|
| i=2 | * | * | * | * | |
| i=3 | * | * | * | ||
| i=4 | * | * | |||
| i=5 | * | ||||
| i=4 | * | * | |||
| i=3 | * | * | * | ||
| i=2 | * | * | * | * | |
| i=1 | * | * | * | * | * |
- Similar to Diamond
Python
n = int(input())
for i in range(n,0,-1):
print(' '*(n-i)+'* '*i)
for i in range(1,n+1):
print(' '*(n-i)+'* '*i)18. Hollow Diamond
Python
*
* *
* *
* *
* *
* *
* *
* *
*| i=1 | * | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| i=2 | * | * | |||||||
| i=3 | * | * | |||||||
| i=4 | * | * | |||||||
| i=5 | * | * | |||||||
| i=4 | * | * | |||||||
| i=3 | * | * | |||||||
| i=2 | * | * | |||||||
| i=1 | * |
- Similar to Hollow Pyramid except you don't write i = n and ith loop from n-1 for lower-half
Python
n = int(input())
for i in range(1, n + 1):
print(" " * (n - i), end="")
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="")
else:
print(" ", end="")
print()
for i in range(n-1, 0,-1):
print(" " * (n - i), end="")
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="")
else:
print(" ", end="")
print()