Autobiographical Number In Python
Published June 26, 2023 by Jarvis Silva
In this tutorial we will see how to check if a number is autobiographical or not using python, An autobiographical number is a type of number where each digit in the number tells us how many times that digit appears in the number itself.
Let’s take the number 21200 for example, the first digit in 0 position of the number is two which indicates that there are 2 zeros in the number, then at 1 position there is one which tells us that there is only 1 one in the number,next we have at the 2 position the number two which indicates that there are only 2 two’s in the number.
I hope you understood what are autobiographical numbers few examples of them are 1210, 11122, 102001 etc, now let’s see how to find them in python code.
Python Code To Check Autobiographical Number
def is_autobiographical_number(number):
number_str = str(number)
count = [0] * 10
for digit in number_str:
count[int(digit)] += 1
for i in range(len(number_str)):
if int(number_str[i]) != count[i]:
return False
return True
# Example usage
number = int(input("Enter a number: "))
if is_autobiographical_number(number):
print(number, "is an autobiographical number.")
else:
print(number, "is not an autobiographical number.")
In the code we have a function which takes a number then it does some calculation and returns true if the number is autobiographical or not, you can run this program using this online python compiler, below is an example output.
Enter a number: 2020
2020 is an autobiographical number.
I hope you found this tutorial useful and helpful do share this program with your friends who might need it and if you want updates of our tutorials or free courses on programming then join our telegram channel.
Here are some more python programs for you:
- Adam Number Program In Python
- Python Program To Check Composite Number
- Python Program To Check Even Or Odd Number
Thanks for reading, Have a nice day 🙂