Showing posts with label Autobiographical Number in Java. Show all posts
Showing posts with label Autobiographical Number in Java. Show all posts

Autobiographical Number in Java

An autobiographical number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on. An autobiographical number is like a self-descriptive number. For example, 1210 has 1 zero, 2 ones, 1 two and 0 threes. 3211000 is another example of an autobiographical number. Even the year 2020 is an autobiographical number! Write a program in Java to input a positive number from the user and check whether that number is an autobiographical number or not. 
 import java.io.*;
 class Autobiographical
{
 public static void main(String args[])throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter the number: ");
 int num = Integer.parseInt(br.readLine());
 num = Math.abs(num); 
 int n = num; 
 String s = String.valueOf(num);
 int digit[] = new int[s.length()];
 for(int i = digit.length - 1; i >= 0; i--)
{
 digit[i] = n % 10; 
 n /= 10;
 }
 boolean status = true;
 for(int i = 0; i < digit.length; i++)
{
 int count = 0;
 for(int j = 0; j < digit.length; j++)
{
 if(i == digit[j]) count++; 
 }
 if(count != digit[i])
{
 status = false;
 break;
 }
 }
 if(status)
 System.out.println(num + " is an Autobiographical Number."); 
 else 
 System.out.println(num + " is NOT an Autobiographical Number."); 
 }
 }

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...