BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Autobiographical Number in Java with example

Last Updated: June 18, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to check whether a given number is Autobiographical number or not.

What is Autobiographical Number?

An autobiographical number is a number N such that the first digit of N represents the count of zeroes in N, the second digit represents the count of ones in N, third digit represents the count of twos in N and so on. For example, 1210 is an autobiographical number because:

Number = 1210
Number of zeroes in 1210 = 1 = first digit of Number
Number of ones in 1210 = 2 = second digit of the Number
Number of twos in 1210 = 1 = third digit of the Number
Number of threes in 1210 = 0 = fourth digit of the Number

How to check if a number is Autobiographical Number?

If sum of the digits of the number is equal to the number of digits in the number then that number is an autobiographical number.

Let’s take the same example that we have seen above.

num = 1210
Number of digits in the num = 4
Sum of the digits of the num = 1+2+1+0 = 4
Number of digits in the num ==  Sum of the digits of the num
Hence num 1210 is an autobiographical number.

Program to check Autobiographical Number in Java

import java.util.*;
public class JavaExample
{
  public static void main(String args[])
  {
    Scanner scanner=new Scanner(System.in);
    System.out.print("Enter the number you want to check: ");
    int number = scanner.nextInt();
    scanner.close();
    //copying value of number in a temp variable
    int temp = number;
    String str = String.valueOf(number);
    int numArray[] = new int[str.length()];
    for(int i = numArray.length - 1; i >= 0; i--)
    {
      numArray[i] = temp % 10;
      temp = temp/10;
    }
    boolean found = true;
    for(int i = 0; i < numArray.length; i++)
    {
      int count = 0;
      for(int j = 0; j < numArray.length; j++)
      {
        if(i == numArray[j])
          count++;
      }
      if(count != numArray[i])
      {
        found = false;
        break;
      }
    }
    if(found)
      System.out.println(number + " is an Autobiographical number.");
    else
      System.out.println(number + " is not an Autobiographical number.");
  }
}

Output:

Enter the number you want to check: 1210
1210 is an Autobiographical number.

Output 2:

Enter the number you want to check: 1234
1234 is not an Autobiographical number.

Related Java Programs

  1. Spy Number in Java
  2. Neon Number in Java
  3. Fascinating Number in Java
  4. Automorphic Number in Java
❮ Java TutorialJava Programs ❯

Top Related Articles:

  1. Neon Number in Java with example
  2. Java Program to Calculate average using Array
  3. Sunny Number Program in Java
  4. Tech Number Program in Java
  5. Automorphic Number Program in Java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap