1210
is an autobiographical number because it contains one 0
, two 1s
, and one 2
.import java.util.Arrays;
public class AutobiographicalNumber {
public static boolean isAutobiographical(int num) {
String str = Integer.toString(num);
int[] arr = new int[str.length()];
for (int i = 0; i < arr.length; i++) {
arr[i] = str.charAt(i) - '0';
}
int[] count = new int[10];
for (int i = 0; i < arr.length; i++) {
count[arr[i]]++;
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] != count[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int num = 1210;
if (isAutobiographical(num)) {
System.out.println(num + " is an autobiographical number");
} else {
System.out.println(num + " is not an autobiographical number");
}
}
}
1210 is an autobiographical number
isAutobiographical()
method takes an integer as input and checks if the number is an autobiographical number or not. The method first converts the integer into a string and then extracts the digits into an integer array. digit
itself. If they match for all digits, the method returns true, indicating that the number is autobiographical. Otherwise, it returns false.main
method, we can pass any number to the isAutobiographical()
method to check if it is an autobiographical number or not.