Course Content

Python Problem 4: Solution | Python Tutorials For Absolute Beginners In Hindi #110

This is the solution to Python Problem #4. Hope you enjoyed doing it. The problem statement is also given below for your convenience.

Problem Statement:-

A palindrome is a string that, when reversed, is equal to itself. Example of the palindrome includes:

676, 616, mom, 100001.

You have to take a number as an input from the user. You have to find the next palindrome corresponding to that number. Your first input should be the number of test cases and then take all the cases as input from the user.

Input:

3

451

10

2133

Output:

Next palindrome for 451 is 454

Next palindrome for 10 is 11

Next palindrome for 2311 is 2222

Code as described/written in the video

'''
Author: Harry
Date: 15 April 2019
Purpose: Practice Problem For CodeWithHarry Channel 
'''


def next_palindrome(n):
    n = n+1
    while not is_palindrome(n):
        n += 1
    return n


def is_palindrome(n):
    return str(n) == str(n)[::-1]


if __name__ == "__main__":
    n = int(input("Enter the number of test cases\n"))
    numbers = []
    for i in range(n):
        number = int(input("Enter the number:\n"))
        numbers.append(number)

    for i in range(n):
        print(
            f"Next palindrome for {numbers[i]} is {next_palindrome(numbers[i])}")

Be the first person to comment!

Comments(0)

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here