How do you print a range of palindrome numbers in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a range of numbers, print all palindromes in the given range. For example if the given range is {10, 115}, then output should be {11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111}
    We can run a loop from min to max and check every number for palindrome. If the number is a palindrome, we can simply print it. 

    Implementation:

    C++

    #include

    using namespace std;

    int isPalindrome[int n]

    {

        int rev = 0;

        for [int i = n; i > 0; i /= 10]

            rev = rev*10 + i%10;

        return [n==rev];

    }

    void countPal[int min, int max]

    {

        for [int i = min; i None:

        for i in range[minn, maxx + 1]:

            if isPalindrome[i]:

                print[i, end = " "]

    if __name__ == "__main__":

        countPal[100, 2000]

    C#

    using System;

    class GFG

    {

    public static int isPalindrome[int n]

    {

        int rev = 0;

        for [int i = n; i > 0; i /= 10]

        {

            rev = rev * 10 + i % 10;

        }

        return [n == rev] ? 1 : 0;

    }

    public static void countPal[int min, 

                                int max]

    {

        for [int i = min; i 0; i /= 10]

        {

            rev = [[rev*10] + [Math.trunc[i]%10]];

            }

        return [n==rev];

    }

    function countPal[min,  max]

    {

        for [var i = min; i 0]: dig=n%10 rev=rev*10+dig n=n//10 if[temp==rev]: print["The number is a palindrome!"] else: print["The number isn't a palindrome!"] 1. User must first enter the value of the integer and store it in a variable.

    How do you find the range of a palindrome number?

    Take two inputs range 1 and range2 for starting the program and also initialize num1 and num2..
    int range1, range2; System. ... .
    for [int i = range1; i

    Chủ Đề