In this tutorial you will learn about the Program to check Autobiographical Number in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Autobiographical Numbers in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Autobiographical Number
A number is called An autobiographical number if first digit counts how many zeroes are there in number,and second digit counts number of twos init, and so on. Basically, it counts the frequency of digits from 0 to 9.
Example: 1210 it has 1 zero, 2 ones, 1 two, and 0 threes.
Another way we can calculate autobiographical number find the sum of all digits (2+1+2+0+0) i.e.5, and after that counts the number of digits of the given number i.e. 5.if both are equal. Hence, the number 21200 is an autobiographical number.
Program to check Autobiographical Number in Java
In this our program we will find given number is a Autobiographical number or not in using java programming. We would first declared and initialized the required variables. Next, we would prompt user to input the a number.Later we find number is Autobiographical number or not let’s have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import java.util.*; public class Autobiographical{ public static void main(String args[]) { int no, number; String str; boolean flag; Scanner sc = new Scanner(System.in); System.out.print("Enter any number: "); no = sc.nextInt(); no = Math.abs(no); number = no; str = String.valueOf(no); int digitarray[] = new int[str.length()]; for (int i = digitarray.length - 1; i >= 0; i--) { digitarray[i] = number % 10; number = number / 10; } flag = true; for (int i = 0; i < digitarray.length; i++) { int count = 0; for (int j = 0; j < digitarray.length; j++) { if (i == digitarray[j]) count++; } if (count != digitarray[i]) { flag = false; break; } } if (flag) System.out.println(no + " is an Autobiographical number."); else System.out.println(no + " is not an Autobiographical number."); } } |
Output
Autobiographical number
Not a Autobiographical number