Saturday, 19 October 2024

Python program to check whether number is palindrome or not also count number of occurrences of each digit.



First of all, we have to write an algorithm for the same so that we come to know how to solve this problem statement step by step.

Algorithm:

1. Start
2. Accept any number n from the user.
3. Using function reverse the accepted number n.
4. Check whether the original and reverse number are same if yes then given number is palindrome.
5. If original and reverse number both are not same, then number is not palindrome.
6. Using function compute each digit occurrence inside the accepted number n.
7. Stop

Now, start the implementation of above algorithm step by step using python.

n = int (input ("Enter the Number n: "))

rev = reverse_num(n) # Function calling to reverse the given number.

if (rev = = n):
    print ("The number is palindrome")
else:
    print ("The number is not palindrome")

ocuurance_of_each_digit(n): # Function calling to find occurrence of each digit.

----------------------------------------------------------------------------------------------------------------------
# Function definition: To reverse the number.

def reverse_num(num):
    reverse = []
    while num >= 1:
        rem = int (num % 10)
        reverse.append(rem)
        num = num / 10
    print(reverse) # get number in reverse order but in list, so we want convert list to str
    temp=""
    for no in reverse:
            temp = temp + str(no)
    rev1 = int (temp) # now converting string to integer
    return rev1
-----------------------------------------------------------------------------------------------------------------------
 

----------------------------------------------------------------------------------------------------------------------
# Function definition: To find the occurrence of each digit in given number.

def ocuurance_of_each_digit(num):
    count = [0,0,0,0,0,0,0,0,0,0]
    while n >= 1:
        digit = int (n % 10)
        count[digit] = count[digit] + 1
        n = n / 10
length = len(count)
i = 0
while i < length:
    if count[i]! = 0:
        print(f"{i} occurance is:", count[i])
    i = i+1
-----------------------------------------------------------------------------------------------------------------------
        
       
    
    

data structures and algorithms Web Developer

No comments:

Post a Comment