176k views
1 vote
A number is said to be Automorphic number if its square ends in the same digits as the number itself.e.g. 5 and 25 appear at the end of their squares 25 and 625 respectively. Hence, they are automorphic numbers. Write a program to accept a number. Pass it to a function that returns the

same number of digits from its square as many digits are available in the argument. Finally display a message whether the number is an automorphic number or not.​

User Mentalhead
by
7.7k points

2 Answers

2 votes
Here's a Python program that accepts a number, checks if it is an automorphic number, and returns the same number of digits from its square as many digits are available in the argument:

```
def automorphic(num):
square = num * num
digits = len(str(num))
if str(square)[-digits:] == str(num):
return str(square)[-digits:]
else:
return "Not automorphic"

num = int(input("Enter a number: "))
result = automorphic(num)
print("The same number of digits from its square as many digits are available in the argument: ", result)
if result == "Not automorphic":
print(num, "is not an automorphic number.")
else:
print(num, "is an automorphic number.")
```

Here's how the program works:

1. The `automorphic()` function takes a number as an argument.
2. It calculates the square of the number using the `*` operator and stores it in the `square` variable.
3. It calculates the number of digits in the original number using the `len()` function and stores it in the `digits` variable.
4. It then checks if the last `digits` digits of the square are equal to the original number by converting both to strings and comparing them using the `==` operator.
5. If they are equal, it returns the last `digits` digits of the square by slicing the string using the `[-digits:]` notation.
6. If they are not equal, it returns the string `"Not automorphic"`.
7. In the main program, the user is prompted to enter a number, which is stored in the `num` variable.
8. The `automorphic()` function is called with the `num` variable as an argument, and the result is stored in the `result` variable.
9. The program then prints the result, which is the same number of digits from the square as many digits are available in the argument.
10. Finally, the program checks if the result is equal to the string `"Not automorphic"`, and prints a message indicating whether the original number is an automorphic number or not.
User NateSHolland
by
8.7k points
5 votes

Answer:

Sure, here's the code:

```

def automorphic(num):

square = str(num**2)

if square[-len(str(num)):] == str(num):

return True

else:

return False

num = int(input("Enter a number: "))

if automorphic(num):

print(num, "is an automorphic number")

else:

print(num, "is not an automorphic number")

```

In this code, we define a function `automorphic` that takes in a number `num`, computes its square and checks if the last `n` digits of the square match the original number `num`, where `n` is the number of digits in `num`. If the last `n` digits match, then the function returns `True`, indicating that `num` is an automorphic number. Otherwise, it returns `False`.

We then accept a number from the user using `input`, pass it to the `automorphic` function and display an appropriate message based on the result.

User NhatNienne
by
8.8k points

Related questions

asked Jan 3, 2020 212k views
Ayusman asked Jan 3, 2020
by Ayusman
7.5k points
1 answer
2 votes
212k views