Automorphic Number TCS DIGITAL Program Asked 2














































Automorphic Number TCS DIGITAL Program Asked 2



Statement: John and Sarah were playing a game in which they have to tell a number which gives the same last digits as of number after squaring that number or basically we have to check whether a number is automorphic or not.

Example1:
Input : N = 25
Output: True
Explanation: As 25*25 = 625

Example 2:
Input : N = 7
Output: False
Explanation: As 7*7 = 49

Algorithm :
1. Store the square of a given number.
2. Loop until N becomes 0 as we have to match 
   all digits with its square.
    i) Check if (n%10 == sq%10) i.e. last digit 
       of number = last digit of square or not
        a) if not equal, return false.
    ii) Otherwise continue i.e. reduce number and 
        square i.e. n = n/10 and sq = sq/10.
3. Return true if all digits matched.

CODE :

def isAutomorphic(N) : 
  
    # Store the square 
    sq = N * N 
       
    # Start Comparing digits 
    while (N > 0) : 
  
        # Return false, if any digit of N doesn't 
        # match with its square's digits from last 
        if (N % 10 != sq % 10) : 
            return False
    
        # Reduce N and square 
        N /= 10
        sq /= 10
    
    return True

This Article Is Written By Rohit Bansal.



Comments

  • Nisrin
    25-Feb-2021 05:03:13 PM
    It should be integer division ( floor division)
    N //= 10
    sq //= 10
    Only division will give float type output and integer division ( floor division) gives int type output. And here in this problem of finding  Automorphic Number it should be  integer division ( floor division).
    Thanks