# Check Whether a Number is an Autobiographical Number or Not in JAVA

A number is said to be an Autobiographical number, if the number’s first digit represents the number of 0 available in the given number, the second number represents the number of 1 present in the given number, the third number represents the number of 2 present in the given number and so on.

In simple terms, if the given number’s digits from left to right represent the frequency of 0, 1, 2, 3, 4.... N respectively present in the given number then that number is known as Autobiographical number.

Some examples of Autobiographical numbers are: 1210, 2020, 21200, 3211000, 42101000 ... etc.

## To show you some instances −

### Instance-1

Input number is 1210.
Let’s check it by using the logic of Autobiographical numbers.
Number of zeros available in the given number is = 1. And the first digit is also 1.
Number of one’s available in the given number is= 2. And the second digit is also 2.
Number of two available in the given number is= 1. And the third digit is also 1.
Number of three’s available in the given number is= 0. And the fourth digit is also 0.
As we notice here by arranging all those digits, we will get the same original number.
Hence, 1210 is an Autobiographical number.


### Instance-2

Input number is 12312.
Let’s check it by using the logic of Autobiographical numbers.
Number of zeros available in the given number is = 0, but the first digit is 1.
Hence, 12312 is not an Autobiographical number.


### Algorithm

Step-1 − Get the input number by static input method.

Step-2 − Convert the input number into string.

Step-3 − Declare an array and then store that string’s digits into the array.

Step-4 − Then initiate the loop and check the number of occurrences of 0, 1, 2, 3, 4 ... n.

Step-5 − If by combining the number of occurrences of the digits and we get the same number as original, then print the given number is an Autobiographical number else not.

### Syntax

To get the absolute value of any number raised to the power of another number in Java we have inbuilt java.lang.Math.abs () method.

Following is the syntax to get power of 2 by using the method −

int value = Math.abs (inputValue)


### Multiple Approaches

We have provided the solution in different approaches.

• By User Defined Method with Static Input Values.

• By Using User Input Value.

Let’s see the program along with its output one by one.

## Approach-1: By Using User Defined Method with Static Input Value

In this approach, we declare a variable and initialize a number as value and pass this number as parameter in a user defined method, then inside the method by using the algorithm we can check whether the number is an Autobiographical number or not.

## Example

import java.util.*;
public class Main {
public static void main(String args[]) {
int inp= 3211000;
if(checkAutobiographical(inp))
System.out.println(inp + " is an autobiographical number.");
else
System.out.println(inp + " is not an autobiographical number.");
}
public static boolean checkAutobiographical(int n){
int inputNumber = Math.abs(n);
int temp = inputNumber;
String s = String.valueOf(inputNumber);
int arr[] = new int[s.length()];
for(int i = arr.length - 1; i >= 0; i--) {
arr[i] = temp % 10;
temp = temp/10;
}
boolean f = true;
for(int i = 0; i < arr.length; i++) {
int count = 0;
for(int j = 0; j < arr.length; j++) {
if(i == arr[j])
count++;
}
if(count != arr[i]) {
f = false;
break;
}
}
if(f)
return true;
else
return false;
}
}


## Output

3211000 is an autobiographical number.


## Approach-2: By Using User Input Value

In this approach, we declare an input number by user input and by using the algorithm we can check whether the number is an Autobiographical number or not

## Example

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
int inputNumber = sc.nextInt();
inputNumber = Math.abs(inputNumber);
int temp = inputNumber;
String s = String.valueOf(inputNumber);
int arr[] = new int[s.length()];
for(int i = arr.length - 1; i >= 0; i--) {
arr[i] = temp % 10;
temp = temp/10;
}
boolean f = true;
for(int i = 0; i < arr.length; i++) {
int count = 0;
for(int j = 0; j < arr.length; j++) {
if(i == arr[j])
count++;
}
if(count != arr[i]) {
f = false;
break;
}
}
if(f)
System.out.println(inputNumber + " is an autobiographical number.");
else
System.out.println(inputNumber + " is not an autobiographical number.");
}
}


## Output

Enter the number: 2020
2020 is an autobiographical number.


In this article, we explored how to check whether a number is an Autobiographical number or not in Java by using different approaches.

Updated on: 27-Dec-2022

7K+ Views

Get certified by completing the course