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


A number is said to be a narcissistic number, if the addition value of each digit raised to the power of numbers of digits available in the original number is equal to the original number.

For more clarification, if the original number has ‘n’ number of digits, first we have to calculate the nth power of each digit. Then add those value and check if that calculated value and original number both are same then the original number is called as narcissistic number.

Some examples of narcissistic numbers are: 153, 8208, 4210818, ... etc.

In this article, we will see how to check if a number is a narcissistic number by using Java programming language.

To show you some instances

Instance-1

Input number is 153.

Let’s check it by using the logic of Narcissistic number.

Number of digits available in 153 is 3.

Calculate the power values and add those = (1^3) + (5^3) + (3^3) = 1 + 125 + 27 = 153

As we notice here both the calculated number and original number are same.

Hence, 153 is a narcissistic number.

Instance-2

Input number is 8208.

Let’s check it by using the logic of Narcissistic number.

Number of digits available in 8208 is 4.

Calculate the power values and add those = (8^4) + (2^4) + (0^4) + (8^4) = 4096 + 16 + 0 + 4096 = 8208

As we notice here both the calculated number and original number are same.

Hence, 8208 is a narcissistic number.

Instance-3

Input number is 250.

Let’s check it by using the logic of Narcissistic number.

Number of digits available in 250 is 3.

Calculate the power values and add those = (2^3) + (5^3) + (0^3) = 8 + 125 + 0 = 133.

As we notice here both the calculated number and original number are not same.

Hence, 250 is not a narcissistic number.

Algorithm

Step-1 - Get the input number by static input method.

Step-2 - Calculate the number of digits of the original number.

Step-3 - Initiate a loop to extract the digits and calculate the power value of it. Inside that loop add those calculated power values and store it into a variable.

Step-4 - Compare both the calculated value with the original number.

Step-5 - If the both are same, then the input number is called a narcissistic number else not.

Syntax

To get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.

Following is the syntax to get power of 2 by using the method -

double power = Math.pow (inputValue,2)

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value with User Defined Method

  • By Using User Defined Method with User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, we declare a variable and initialize it with a number. Call a user defined method by passing this number as a parameter. Then inside the method by using the algorithm we can check whether the number is a narcissistic number or not.

Example

import java.util.*; 
import static java.lang.Math.*;
public class Main {
   public static void main(String args[]) {

      //declare a variable and initialize it with a number
      int inputNumber = 8208;
      
      //call the function inside the if condition which checks narcissistic
      if (checkNarcissistic(inputNumber))
         System.out.println(inputNumber + " is a narcissistic number.");
      else
         System.out.println(inputNumber + " is not a narcissistic number.");
   }
   
   //user-defined method to count the digits
   static int countDig(int num) {
      if (num == 0)
         return 0;
         return 1 + countDig(num / 10);
   }
   
   //user-defined method to check narcissistic number
   static boolean checkNarcissistic(int num) {
      
      //declare a variable and store the counts of digits
      int p = countDig(num);
      
      //declare a variable and temporary store the input number
      int temp = num;
      
      //Declare a variable which store the sum value and initiate it with 0
      int sum = 0;
      
      //execute the loop
      while(temp > 0) {
         
         //extarct digits and find power of it
         
         //continuously add it to sum value
         sum+= pow(temp % 10, p);
         
         //removes the digit which is already calculated
         temp = temp / 10;
      }
      
      //return true if original number is equal to the sum value
      return (num == sum);
   }
}

Output

8208 is a narcissistic number.

Approach-2: By Using Static Input Value

In this approach, we declare a variable and assign a value to it by user input. Call a user defined method by passing this number as a parameter. Then inside the method by using the algorithm we can check whether the number is a narcissistic number or not.

Example

import java.util.*;
import static java.lang.Math.*;
public class Main {
   public static void main(String args[]) {
      
      //create object of Scanner class
      Scanner sc = new Scanner(System.in);
      
      //ask the user to enter a number
      System.out.print("Enter the number: ");
      
      //declare a variable and store the input value
      int inputNumber = sc.nextInt();
      
      //call the function inside the if condition which checks narcissistic
      if (checkNarcissistic(inputNumber))
         System.out.println(inputNumber + " is a narcissistic number.");
      else
         System.out.println(inputNumber + " is not a narcissistic number.");
   }
   
   //user-defined method to count the digits
   static int countDig(int num) {
      if (num == 0)
         return 0;
         return 1 + countDig(num / 10);
   }
   
   //user-defined method to check narcissistic number
   static boolean checkNarcissistic(int num) {
      
      //declare a variable and store the counts of digits
      int p = countDig(num);
      
      //declare a variable and temporary store the input number
      int temp = num;
      
      //Declare a variable which store the sum value and initiate it with 0
      int sum = 0;
      
      //execute the loop
      while(temp > 0) {
         
         //extarct digits and find power of it
         
         //continuously add it to sum value
         sum+= pow(temp % 10, p);
         
         //removes the digit which is already calculated
         temp = temp / 10;
      }
      
      //return true if original number is equal to the sum value
      return (num == sum);
   }
} 

Output

Enter the number: 8208
8208 is a narcissistic number.

In this article, we explored how to check a number whether it is a narcissistic number or not in Java by using different approaches.

Updated on: 09-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements