# Extravagant Number in Java

In this section, we will learn what is extravagant number and also create Java programs to check if the given number is an extravagant number or not. The extravagant number Java program frequently asked in Java coding interviews and academics.

## Extravagant Number

A natural number that has fewer digits than the number of digits present in its prime factorization (including exponents) is known as the extravagant number. It is also known as the wasteful numbers because of this property.

Extravagant numbers are the subset of composite numbers because one can factorize the extravagant numbers. But vice-versa is not true.

Steps to Find Extravagant Numbers

Step 1: Pick a natural number. Let's say the number is n.

Step 2: Compute the prime factorization of the number n. Store the digits involved in the prime factors of the prime factorization and its exponents in an array.

Step 3: Find the size of the array.

Step 4: Calculate the number of digits present in the number n.

Step 5: If the total number of digits present in the number n is less than the size of the array found in step 3, then the number n is an extravagant number; otherwise, not.

## Examples of Extravagant Numbers

Extravagant Number Prime Factorization Number of digits in the number Number of digits in the prime factorization (including exponents)
4 2 * 2 1 2
6 2 * 3 1 2
8 2 * 2 * 2 1 3
9 3 * 3 1 2
12 2 * 2 * 3 2 3
18 2 * 3 * 3 2 3
20 2 * 2 * 5 2 3
22 2 * 11 2 3 (two digits of 11)

We see that for every number mentioned, the number of digits is less than the number of digits present in the prime factorization (including exponents). Hence, the numbers mentioned in the above table are extravagant numbers.

## Extravagant Numbers Java Program

The following program checks for extravagant numbers using the steps defined above.

FileName: ExtravagantNumber.java

Output:

1 is not an extravagant number.
2 is not an extravagant number.
3 is not an extravagant number.
4 is an extravagant number.
5 is not an extravagant number.
6 is an extravagant number.
7 is not an extravagant number.
8 is an extravagant number.
9 is an extravagant number.
10 is not an extravagant number.
11 is not an extravagant number.
12 is an extravagant number.
13 is not an extravagant number.
14 is not an extravagant number.
15 is not an extravagant number.
16 is not an extravagant number.
17 is not an extravagant number.
18 is an extravagant number.
19 is not an extravagant number.
20 is an extravagant number.
21 is not an extravagant number.
22 is an extravagant number.
23 is not an extravagant number.
24 is an extravagant number.
25 is not an extravagant number.
26 is an extravagant number.
27 is not an extravagant number.
28 is an extravagant number.
29 is not an extravagant number.
30 is an extravagant number.

Next TopicJava Unicode