Some more python programs on patterns

Print the following patterns for the given number of rows.


Pattern 1

Pattern for N = 5
 1    2   3    4   5
 11   12  13   14  15
 21   22  23   24  25
 16   17  18   19  20
 6    7    8   9   10
Input Format :
Total 'N' number of rows
Output Format :
 Pattern in 'N'number of rows
Sample Input :
4
Sample Output :
 1  2  3  4
 9 10 11 12
13 14 15 16
 5  6  7  8

Solution :

  1. N = int(input())
  2. a = 1
  3. for i in range(1, N + 1):
  4. for j in range(a, a + N):
  5. print(j, end=" ")
  6. print()
  7. if i == (N + 1)//2:
  8. if N % 2!=0:
  9. a = N*(N - 2) + 1
  10. else:
  11. a = N*(N - 1) + 1
  12. elif (i>(N + 1)//2):
  13. a = a - 2*N
  14. else:
  15. a = a + 2*N


Pattern 2

Pattern for N = 6
123456
  23456
    3456
      456
        56
          6
        56
      456
    3456
  23456
123456
Input Format :
Total 'N' number of rows
Output Format :
 Final pattern
Sample Input :
4
Sample Output :
1234
  234
    34
      4
    34
  234
1234

Solution :

  1. N = int(input())
  2. i = 1
  3. while i<= N:
  4. if i == 1:
  5. j = 2
  6. else:
  7. j = 1
  8. while j <= i - 1:
  9. print(' ', end=(''))
  10. j = j + 1
  11. j = i
  12. while j <= N:
  13. print(j, end=(''))
  14. j = j + 1
  15. print()
  16. i = i + 1
  17. i = 1
  18. while i <= N - 1:
  19. j = N - 2
  20. while j >= i:
  21. print(' ', end=(''))
  22. j = j - 1
  23. a = N - i
  24. j = 0
  25. while j <= i:
  26. print(a, end=(''))
  27. j = j + 1
  28. a = a + 1
  29. print()
  30. i = i + 1

Try it >>
Previous Post Next Post