A number is called elegant if for all 0 d 9 the digit d does

A number is called elegant if, for all 0 d 9, the digit d does not appear more than d times. For example, the number 3 cannot appear more than 3 times in an elegant number (and the digit 0 cannot appear at all). How many positive 4-digit elegant numbers exist? Some numbers to include are 1345, 5999, 6554, and 7828.

Solution

Hi Friend, I have written Java code to count number of elegant number.

Please let me know in case of any issue.

public class ElegantNumberCount {

  

   public static boolean isElegantNumber(int number){

      

       // array to store the number of count of each digit

       int[] digitCount = new int[10];

      

       while(number > 0){

          

           int digit = number%10;

           number = number/10;

          

           digitCount[digit]++;

       }

      

       // if any digit count is greater than number then return false

       for(int i=0; i<10; i++){

          

           if(digitCount[i] > i)

               return false;

       }

      

       return true;

   }

  

   public static void main(String[] args) {

      

       int count = 0;

      

       for(int i=1000; i<=9999; i++){

          

           if(isElegantNumber(i))

               count++;

       }

      

       System.out.println(\"Total number elegant numbers in 4-digit numbers: \"+count);

   }

}

/*

Sample run:

Total number elegant numbers in 4-digit numbers: 6110

*/

A number is called elegant if, for all 0 d 9, the digit d does not appear more than d times. For example, the number 3 cannot appear more than 3 times in an ele
A number is called elegant if, for all 0 d 9, the digit d does not appear more than d times. For example, the number 3 cannot appear more than 3 times in an ele

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site