Python program to print palindrome triangle with n lines


Suppose we have a number n. We have to find a triangle with n rows and each row contains palindrome.

So, if the input is like n = 5, then the output will be

1
121
12321
1234321
123454321

To solve this, we will follow these steps −

  • for i in range 1 to n, do
    • display ((integer part of (10^i) - 1)/9)^2
    • go to next line

Example

Let us see the following implementation to get better understanding −

def solve(n):
   for i in range(1,n+1):
      print((((10**i) - 1)//9)**2)

n = 8
solve(n)

Input

8

Output

1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321

Updated on: 12-Oct-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements