Palindrome numbers between 1 to 100 in python


Tutorialsrack 23/04/2020 Python

In this Python Program, we will learn how to print Palindrome Numbers from 1 to 100 or between a specific range.

What is Palindrome?

A palindrome is a word, number, phrase, or other sequences of characters that reads the same backward as forward, such as civic or rotator or the number 14241. 

A number is a palindrome when we reverse the number and the reversed number is equal to the original number.

Here is the code of the program to print Palindrome Numbers from 1 to 100 or between a specific range.

# Python Program to print Palindrome numbers from 1 to 100

# Take the Input from the User
minValue = int(input("Enter the Minimum Value : "))
maxValue = int(input("Enter the Maximum Value : "))

print("Palindrome Numbers between %d and %d are : " %(minValue, maxValue))
for num in range(minValue, maxValue + 1):
    temp = num
    reverse = 0
    
    while(temp > 0):
        Reminder = temp % 10
        reverse = (reverse * 10) + Reminder
        temp = temp //10

    if(num == reverse):
        print("%d " %num, end = '  ')

Enter the Minimum Value : 1

Enter the Maximum Value : 100

Palindrome Numbers between 1 and 100 are : 

1   2   3   4   5   6   7   8   9   11   22   33   44   55   66   77   88   99   

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 <= max; i++)

            if (isPalindrome(i))

              cout << i << " ";

    }

    int main()

    {

        countPal(100, 2000);

        return 0;

    }

    Java

    class GFG

    {

        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;

        }

        static void countPal(int min, int max)

        {

            for (int i = min; i <= max; i++)

                if (isPalindrome(i)==1)

                    System.out.print(i + " ");

        }

        public static void main(String args[])

        {

            countPal(100, 2000);

        }

    }

    Python3

    def isPalindrome(n: int) -> bool:

        rev = 0

        i = n

        while i > 0:

            rev = rev * 10 + i % 10

            i //= 10

        return (n == rev)

    def countPal(minn: int, maxx: int) -> 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 <= max; i++)

        {

            if (isPalindrome(i) == 1)

            {

                Console.Write(i + " ");

            }

        }

    }

    public static void Main(string[] args)

    {

        countPal(100, 2000);

    }

    }

    Javascript

    Output

    101 111 121 131 141 151 161 171 181 191 202 212 222 232 242
     252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 
     414 424 434 444 454 464 474 484 494 505 515 525 535 545 555 565 
     575 585 595 606 616 626 636 646 656 666 676 686 696 707 717 727 
     737 747 757 767 777 787 797 808 818 828 838 848 858 868 878 888
     898 909 919 929 939 949 959 969 979 989 999 1001 1111 1221 1331 
     1441 1551 1661 1771 1881 1991 

    Time Complexity:

    Time complexity of function to check if a number N is palindrome or not is O(logN).

    We are calling this function each time while iterating from min to max.

    So the time complexity will be O(Dlog(M)).

    Where,

    D= max-min

    M = max

    Auxiliary Space: O(1)


    How many palindromic numbers are there between 1 and 100?

    The first 30 palindromic numbers (in decimal) are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, … (sequence A002113 in the OEIS). Palindromic numbers receive most attention in the realm of recreational mathematics.

    How do you find the palindrome of a number in Python?

    n=int(input("Enter number:")) temp=n rev=0 while(n>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!")

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

    Approach:.
    Give the lower limit range as static input and store it in a variable..
    Give the upper limit range as static input and store it in another variable..
    Loop from lower limit range to upper limit range using For loop..
    Inside the loop, give the iterator value as the number of the for loop..

    How do I print a palindrome?

    #include.
    int main().
    int n,r,sum=0,temp;.
    printf("enter the number=");.
    scanf("%d",&n);.
    temp=n;.
    while(n>0).