Javatpoint Logo
Javatpoint Logo

Autobiographical Number in Java

In this section, we will learn what is an autobiographical number and also create Java programs to check if the given number is autobiographical or not. The autobiographical number program frequently asked in Java coding tests to check the logic of the programmer.

Autobiographical Number

A digit count numerically précises the frequency of digits 0 through 9 in the order in which they occur in a number. In other words, a number that is a pompous and self-centered figure whose only purpose in life is to describe itself.

We can define an autobiographical number formally as:

An autobiographical number is a number (N) such that the first digit of N counts how many zeroes are presented in N, the second digit counts how many ones are presented in N, the third digit counts how many twos are presented in N, and so on. Sometimes, it is also known as a curious number. It is the sequence OEIS A046043.

It is just like Self-describing primes that uses a different method OEIS A108810 to describe the Self-describing primes. The only difference between the two is that in autobiographical numbers digits are described in increasing order while self-describing primes digits can be described in any order.

There is only a number 10213223 that describes itself. We observe that the frequency of 0 is 1, the frequency of 1 is 2, the frequency of 2 is 3, and the frequency of 3 is 2. Therefore, 10213223 is only a number that describes itself. The smallest autobiographical number is 1210.

Autobiographical Number Example

Suppose, we want to check the number 21200 is an autobiographical number or not.

First, find the sum of its digits (2+1+2+0+0) i.e. 5, and counts the number of digits of the given number i.e. 5. We observe that both are equal. Hence, the number 21200 is an autobiographical number.

Note: The method discussed above works only if the sum of digits is less than 10.

Let's take another number 1210. It has 1 zero, 2 ones, 1 two, and 0 threes. Hence, the number 21200 is an autobiographical number.

The above method can be used for all numbers.

Some autobiographical numbers are: 1210, 2020, 21200, 3211000, 42101000, 521001000, 6210001000.

Steps to Find Autobiographical Number

For each digit from 0 to 9 that is present in the number, write down the frequency of that digit and then the digit. After writing the frequency and digit the number that we get is called an autobiographical number.

Let's find all autobiographical numbers by using the zoom-in method.

  1. By the above description, the autobiographical number cannot have more than 10 digits. So, it is clear that the autobiographical numbers cannot be too large.
  2. In an autobiographical number, the sum of digits equals the number of digits. Remember that the sum of digits should not exceed 10. For example, 2020.
  3. As we have discussed above, the 1st digit counts the frequency of 0's. We also know that self-describing numbers do not start with zeros. Hence, the number of zeroes is not zero.
  4. Subtracting step 3 from step 2 above, we get a resulting statement that the sum of all the digits, except for the first one, is equal to the number of non-zero digits plus 1.
  5. It means that except for the first digit, the set of all other non-zero digits consists of several 1's and one 2.
  6. Exclusively, in the autobiographical numbers, the number of 1' is either 0, 1, or 2.

Now we will use the zooming-in method in three different ways depending on the number of 1's. In this section, we will consider only the case if there are no 1's in the number.

  • If the number of 1's is zero, the only non-zero non-first digit of such a will be 2.
  • The number (i.e. 2) will be included in the autobiographical number. Therefore, the 3rd digit of the number will not zero. Since it will be 2.
  • The number contains 2 twos.
  • Hence, the autobiographical number will be 2020.

Let's implement the above logic in a Java program.

Autobiographical Number Java Program

AutobiographicalNumberExample.java

Output 1:

Enter the number you want to check: 1210
1210 is an Autobiographical Number.

Output 2:

Enter the number you want to check: 2120001
2120001 is not an autobiographical number.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA