Autobiographical Number in Java

(Last Updated On: 13/10/2023)

An “autobiographical number,” derived from the word “autobiography,” possesses a unique property where it reflects its own digits. In this blog post, we’ll look at the fascinating world of autobiographical numbers in Java and learn how to identify them with code.

What are the Autobiographical Numbers in Java?

An autobiographical number is a type of number in which the digit at a specific position represents the number of times that digit appears in the entire number. Simply put, if we have an n-digit number, the first digit represents the number’s count of zeros, the second digit represents the number’s count of ones, and so on.

Autobiographical Numbers in Java Example

Example 1:

21200

In this example, the number has two instances of the digit 0, as the first digit is 2, indicating that it. The second digit, which is 1, indicates one occurrence of the digit 1. The third digit is 2, which denotes the presence of the digit twice. The final two digits are both 0, indicating that neither the numbers 3 nor 4 were present. Consequently, this number qualifies as an autobiographical number.

Example 2: 

6210001000

As mentioned earlier, this number is an autobiographical number. And below is a breakdown:

The first digit is 6, representing six zeros in the number.

The second digit is 2, indicating two ones.

The third digit is 1, signifying one occurrence of the digit 2.

The fourth digit is 0, representing no threes.

The fifth digit is also 0, indicating no fours.

The remaining digits (all zeros) represent no occurrences of the digits 5, 6, 7, 8, and 9.

Example 3: 

1210

In this example, the first digit, 1, signifies the number’s first zero. The second digit, 2, represents two ones. The third digit is 1, signifying one instance of the number 2. The final digit, 0, represents a lack of threes. The number of fours in the number is not matched by a digit, though. As a result, this number is not autobiographical. 

Remember, Autobiographical Numbers have a unique property where the digit at a certain position reflects the count of that digit in the entire number.

Autobiographical Number Detection in Java Implementation:

We can identify autobiographical numbers in Java programs with a simple algorithm. In this order, the steps are:

Step 1: Start by creating a string from the given number.

Step 2: Repeat this process for each digit of the number.

Step 3: Count the instances of each digit throughout the entire number.

Step 4: Check the count against the corresponding digit. Continue to the next digit if they match; otherwise, the number is not an autobiographical number.

Step 5: A number is an autobiographical number if the match test is successful for all digits.

Also, learn about ATM Program in Java here!

Java program that checks whether a given number is an Autobiographical Number or not

import java.util.*;  
public class AutobiographicalNumEx  
{  
public static void main(String args[])   
{  
Scanner sc=new Scanner(System.in);  
System.out.print("Enter the number you want to check: ");  
//read an integer from the user   
int number = sc.nextInt();  
//determines the absolute value   
number = Math.abs(number);    
int n = number;  
//the valueOf() method returns the string representation of int argument  
String str = String.valueOf(number);  
//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 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  
for(int i = 0; i < digitarray.length; i++)  
{  
int count = 0;  
for(int j = 0; j < digitarray.length; j++)  
{  
if(i == digitarray[j])  
//increments 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 true  
System.out.println(number + " is an autobiographical number.");  
else  
//prints if false  
System.out.println(number + " is not an autobiographical number.");  
}  
}  

Explanation of the code:

The number of times a given digit appears overall is represented by each digit in an autobiographical number.

The program converts an integer provided by the user to its absolute value. Following conversion, the number is represented as a string. The program creates an array to store each of the number’s digits separately.

Using nested loops, the program iterates over each digit and compares it to the count of that digit in the number. If there is a mismatch, the flag variable is set to false, indicating that the number is not an autobiographical number. If each digit matches its assigned count, the flag remains true. 

Finally, the program outputs the value of the flag variable, which determines whether or not the given number is autobiographical. To determine whether a given number is an autobiographical number, this code effectively compares the counts of each digit with their respective positions.

Output:

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

Check out our blog on Prime Number in Java here!

Find all Autobiographical Numbers with a given number of digits

// Java implementation to find Autobiographical numbers with length N

import java.util.*;
import java.lang.Math;

public class AutobiographicalEx {
	public static boolean isAutoBio(int num)
	{
		String autoStr;

		int index, number, i, j, cnt;

		// Converting the integer number to string
		autoStr = Integer.toString(num);

		for (i = 0; i < autoStr.length(); i++) {

			// Extracting each characte from each index one by one and converting into an integer
			index = Integer.parseInt(autoStr.charAt(i) + "");

			// initialize count as 0
			cnt = 0;

			for (j = 0; j < autoStr.length(); j++) {
				number = Integer.parseInt(autoStr.charAt(j) + "");

				// Check if it is equal to the index i if true then increment the count
				if (number == i)

					// It is an
					// Autobiographical
					// number
					cnt++;
			}

			// Return false if the count and the index number are not equal
			if (cnt != index)

				return false;
		}

		return true;
	}

	// Function to print autobiographical number with given number of digits
	public static void findAutoBios(double n)
	{
		// both the boundaries are taken double, so as to satisfy Math.pow() function's signature
		double high, low;

		int i, flag = 0;

		// Left boundary of interval
		low = Math.pow(10.0, n - 1);

		// Right boundary of interval
		high = Math.pow(10.0, n) - 1.0;

		for (i = (int)low; i <= (int)high; i++)

			if (isAutoBio(i)) {
				flag = 1;
				System.out.print(i + ", ");
			}

		// Flag = 0 implies that the number is not an autobiographical no.
		if (flag == 0)

			System.out.println("There is no Autobiographical Number"
							+ "with " + (int)n + " digits");
	}

	// Driver Code
	public static void main(String[] args)
	{
		double N = 5;
		findAutoBios(N);

		N = 4;
		findAutoBios(N);
	}
}

Explanation of the code:

Autobiographical Numbers are those numbers where each digit in the number represents the count of that digit in the entire number.

The program defines a function isAutoBio() to check if a given number is an Autobiographical Number. It iterates through each digit in the number and compares it with the count of that digit in the entire number. If any mismatch is found, the function returns false; otherwise, it returns true.

The main function findAutoBios() takes a double value n representing the number of digits. It calculates the range of numbers within that length and checks each number using the isAutoBio() function. If an Autobiographical Number is found, it is printed. If no Autobiographical Number is found within the range, a message is displayed.

In the main() function, the program demonstrates finding Autobiographical Numbers for different lengths by calling findAutoBios() with different values of N.

Overall, this code efficiently checks and prints Autobiographical Numbers of a given length using the isAutoBio() function and the findAutoBios() function.

Get Complete Python Interview Questions and Answers, here!

Output:

21200, 1210, 2020, 

We hope you like our informative blog on “Autobiographical Number in Java” and that it helps you regarding your java related queries. For more exciting blogs on Java and other programming languages keep checking our website Newtum. You can improve your coding skills and expand your programming knowledge.

About The Author

Leave a Reply