How To Check Whether a Number is a Narcissistic Number or Not in Java?



What is a Narcissistic Number?

A number is said to be a Narcissistic number, only if the addition (or sum) of the value of each digit raised to the power of the total number of digits available in the original number is equal to the original number.

Examples of narcissistic numbers, such as 153, 1634, 54748, etc. All single-digit numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) are Narcissistic numbers because the power of 1 of any digit will be the digit itself.

The following input and output scenarios will help you understand the problem and calculation to check the Narcissistic number:

Scenario 1

Suppose the input number is 153:

Input: 250
Output: No
Calculation:
No of digits in the number 153 is = 3
Calculate the power of 3 of each digit and add them = (2^3) + (5^3) + (0^3) = 8 + 125 + 0
result = 133 (i.e., not equal to original number)

The calculated number and the original number are not the same; 250 is not a narcissistic number.

Example 1

In the following example, we will count the number of digits in the number 8208. Then, we will calculate the sum of each digit raised to the power of the total number of digits. If the resulting sum is equal to the original number, then the number is Narcissistic:

public class checkNarcissistic {
   public static void main(String args[]) {
      int inputNumber = 8208;
      System.out.println("The given number is: " + inputNumber);
      int no_of_digits = 0;
      int temp1 = inputNumber; //to count digit
      int temp2 = inputNumber; //to calculate sum
      int sum = 0;
      
      //count digit
      while(temp1 != 0){
         no_of_digits++;
         temp1 = temp1 / 10;
      }
      
      while(temp2 > 0) {
	     //calculate sum
         sum += Math.pow(temp2 % 10, no_of_digits);
         temp2 = temp2 / 10;
      }
      //compare original number with resultant sum
      if (inputNumber == sum){
         System.out.println("Yes! " + inputNumber + " is a narcissistic number.");
      }
      else{
         System.out.println("No!" + inputNumber + " is not a narcissistic number.");
      }
   }
}

The above program produces the following output:

The given number is: 8208
Yes! 8208 is a narcissistic number.

Example 2

In the example below, we define two methods named countDig() and checkNarcissisticNumber() to count the number of digits in the given number 25 and check a Narcissistic number, respectively. If the sum of each digit raised to the power of the total number of digits is not equal to the original number, the number is not Narcissistic:

import java.util.*;
public class checkNarcissistic {
   //method to count digits
   public static int countDig(int num) {
      if (num == 0){
         return 0;
      }
      return 1 + countDig(num / 10);
   }
   
   // method to check narcissistic number
   public static boolean checkNarcissisticNumber(int num) {
      //get the digit
      int no_of_digits = countDig(num);
      int temp = num;
      int sum = 0;
      while(temp > 0) {
         //calculate the sum rasied to power no_of_digits
         sum += Math.pow(temp % 10, no_of_digits);
         temp = temp / 10;
      }
      return (num == sum);
   }
   public static void main(String args[]) {
      int num = 25;
      System.out.println("The given number is: " + num);
      boolean result = checkNarcissisticNumber(num);
      System.out.println("Is the number " + num + " is a Narcissistic number? " + result);
   }   
}

Following is the output of the above program:

The given number is: 25
Is the number 25 is a Narcissistic number? false
Updated on: 2025-08-28T15:47:12+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements