Problem Set 6(Diffadam number)

A number ‘n’ is said to be a Diffadam number if the absolute value of the difference between the number ‘n’ and the reverse of ‘n’ is zero. For example, 121 is Diffadam number. Reverse of a number is the number got by writing the digits of the number in the reverse order (from right to left). Reverse of 132 is 231.

Write an Algorithm and the subsequent Python code to check whether the given number is a Diffadam number or not.

Write a function to reverse a number

Input Format:
The first line will contain the number.

Output Format:

Print Diffadam number or Not a Diffadam number


Input:
The number n

Processing:
def reverse(number):
    rev_num = ''
    temp = number
    while temp:
        rev_num = rev_num + str(temp%10)
        temp = temp//10
    rev_num = int(rev_num)
    return rev_num

Output:
Display whether the number is an Diffadam Number or not

Program:
def reverse(number):
    rev_num = ''
    temp = abs(number)
    while temp:
        rev_num = rev_num + str(temp%10)
        temp = temp//10
    rev_num = int(rev_num)
    return rev_num

n = int(input())
if abs(n - (reverse(n)))==0:
    print('Diffadam number')
else:
    print('Not an Diffadam number')


Algorithm:
Step1. Define the reverse function which has one parameter as number
Step1.1 assign rev_num as an empty string
Step1.2 assign temp as number
Step1.3 repeat till is not equal to zero
Step1.3.1 assign rev_num as the sum of rev_num and the remainder obtained when temp is divided by 10 which is converted into a string
Step1.3.2 let temp be temp divided by 10
Step1.4 get the number n
Step1.5 check if reverse of square of n is equal to reverse of n and take the square if true display Diffadam number else display not an Diffadam number.
Step1.6 End

3 comments:

  1. Replies
    1. made a slight change in the code...check if that code worked...the only issue is that i did not get these questions as part of my skillrack questions and hence couldn't check these questions personally

      Delete

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...