Autobiographical Number in Java

An autobiographical number is a positive integer where the digits in the number represent the frequency of each digit in the number itself. In other words, the number describes itself.

Let’s create a Java program to check if a given number is autobiographical or not. This program is often used in Java coding tests to evaluate a programmer’s logic.

Please note that autobiographical numbers are quite rare, and not all numbers have an autobiographical representation.

Autobiographical Number

An autobiographical number, also known as a curious number, is a special kind of number where each digit represents the frequency of its corresponding digit in the number itself. The digits are arranged in increasing order, starting from 0.

For example, the number 10213223 is an autobiographical number because it describes itself. The first digit (1) represents the frequency of 0s in the number (1 zero), the second digit (0) represents the frequency of 1s (2 ones), the third digit (2) represents the frequency of 2s (3 twos), and so on.

It is worth noting that autobiographical numbers are quite rare, and the number 10213223 is the only known autobiographical number. Additionally, the smallest autobiographical number is 1210.

Autobiographical Number Example

To determine whether a number is an autobiographical number, we can follow a simple method. Let’s consider the number 21200 as an example.

First, we calculate the sum of its digits: 2 + 1 + 2 + 0 + 0 = 5.

Next, we count the number of digits in the given number, which is also 5.

If the sum of the digits is equal to the count of digits, then the number is an autobiographical number. In this case, both are equal, so 21200 is indeed an autobiographical number.

This method can be applied to any number to check if it is autobiographical or not.

Some examples of autobiographical numbers include 1210, 2020, 21200, 3211000, 42101000, and 521001000.

Steps to Find Autobiographical Number

To find autobiographical numbers using the zoom-in method, we can follow these steps:

  1. Autobiographical numbers cannot have more than 10 digits, so they are not excessively large.
  2. In an autobiographical number, the sum of its digits is equal to the number of digits. However, the sum of digits should not exceed 10. For example, in the number 2020, the sum of digits (2+0+2+0) is equal to the number of digits (4).
  3. The first digit of an autobiographical number represents the frequency of zeros. Since self-describing numbers do not start with zeros, the number of zeros should not be zero.
  4. Subtracting the number of zeros (step 3) from the total sum of digits (step 2), we find that the sum of all other non-zero digits is equal to the number of non-zero digits plus 1.
  5. In autobiographical numbers, the set of non-zero digits consists of one or more ones and a single two.

Using these conditions, we can identify autobiographical numbers by checking if they satisfy the specified patterns.

When there are no 1’s in the autobiographical number, we can use the zooming-in method as follows:

  1. If the number of 1’s is zero, the only non-zero non-first digit will be 2.
  2. The number 2 will be included in the autobiographical number. Therefore, the third digit of the number will not be zero, but instead, it will be 2.
  3. Since there are two twos in the number, we include them accordingly.

Hence, the autobiographical number will be 2020.

The given content has been rewritten without plagiarism while retaining the original information.

Autobiographical Number Java Program

AutobiographicalNumberExample.java

import java.util.*;  
public class AutobiographicalNumberExample  
{  
public static void main(String args[])   
{  
Scanner sc=new Scanner(System.in);  
System.out.print("Enter the number you want to check: ");  
//reading an integer from the user to check  
int num = sc.nextInt();  
//determines the absolute value of the given number  
num = Math.abs(num);  
//assigning the value of num into variable n  
int n = num;  
//the valueOf() method returns the string representation of int argument  
String str = String.valueOf(num);  
//creates an array of digits  
int digitarray[] = new int[str.length()];  
for(int i = digitarray.length - 1; i >= 0; i--)  
{  
//determines the last digit of the given number      
digitarray[i] = n % 10;  
//removes the last digit  
n = n/10;  
}  
boolean flag = true;  
//an inner loop compares the iterator of the outer loop with each digit of the inner loop //if they are equal then increment the occurrence count of the digit  
for(int i = 0; i < digitarray.length; i++)  
{  
int count = 0;  
for(int j = 0; j < digitarray.length; j++)  
{  
if(i == digitarray[j])  
//increments the count by 1 if the above condition returns true  
count++;  
}  
if(count != digitarray[i])  
{  
flag = false;  
//breaks the execution if the condition becomes true  
break;  
}  
}  
if(flag)  
//prints if the status returns true  
System.out.println(num + " is an autobiographical number.");  
else  
//prints if status returns false  
System.out.println(num + " is not an autobiographical number.");  
}  
}

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.

Make sure to regularly visit tutorials.freshersnow.com to expand your knowledge, such as learning about Autobiographical Number in Java.