In the previous article, we have discussed Java Program to Check Keith Number
In this article we are going to understand what Autobiographical number is and how we can check whether a number is Autobiographical number in java or not in Java with examples, What is Autobiographical Number Program In Java, Autobiography Number Program, Autobiography Number Program In Java, An Autobiographical Number Program, Autobiographical Number Code In Java, An Autobiographical Number Is A Number are discussed lightly if you observe clearly.
Program to Check Autobiographical Number
Autobiographical number is number where a number N such that the first digit of N counts how many zeroes are in N, the second digit counts how many ones are in N and so on.
Example : 1210 : 1 Zero, 2 Ones, 1 Twos and 0 Threes Autobiographical number 21200 : 2 Zero, 1 Ones, 2 Twos, 0 Threes and 0 Fours Autobiographical number 63 : There is no ones here, Not a Autobiographical number
In the above examples the numbers 1210 and 21200 are Autobiographical numbers. However 63 is not an Autobiographical number.
Program:
import java.util.Scanner; public class AutobiographicalNumber{ public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); // Taking the absolute value of the number num = Math.abs(num); // Preserving the original value int temp = num; // Converting the number to a string String str = String.valueOf(num); int dig[] = new int[str.length()]; // Storing the number in an array for(int i = dig.length-1;i>=0;i--) { dig[i] = temp%10; temp /= 10; } boolean flag = true; // Loop to check if all the digits occur as defined for(int i = 0;i<dig.length;i++) { int count = 0; for(int a = 0; a<dig.length; a++) { if(i==dig[a]) count++; } if(count!=dig[i]) { flag=false; break; } } if(flag) { System.out.println(num+" is an Autobiographical Number"); } else { System.out.println(num+" is Not an Autobiographical Number"); } } }
Output: Enter a number : 1210 1210 is an Autobiographical Number
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Read more: Java Program to Check Keith Number
Answer these:
- An Autobiographical Number Is A Number N Such That The First Digit
- An Autobiographical Number Is A Number N Such That In Python
- Check Autobiographical Number Program In Python
- Autobiographical Number Java
Related Java Programs: