w3resource

Java: Find all the narcissistic numbers between 1 and 1000


Narcissistic Numbers Between 1 and 1000

Write a Java program to find all narcissistic numbers between 1 and 1000.

In number theory, a narcissistic number is a number that is the sum of its own digits each raised to the power of the number of digits.
For example:
153 = 13 + 53 + 33

Pictorial Presentation:

Java: Find all the narcissistic numbers between 1 and 1000.


Sample Solution:

Java Code:

import java.util.LinkedList;
public class Example23  {

    public static void main(String args[])
    {
        for (int i = 1; i < 1000; i++) {
            int n = i;
            LinkedList<Integer> data = new LinkedList<>();
            while (n > 0) {
                data.push( n % 10 );
                n = n / 10;
            }
            int n1 = 0;
            for(Integer num : data) {
                n1 += Math.pow(num, data.size());
            }
            if(i == n1) {
                System.out.println(i);
            }
        }
    }
}

Sample Output:

1                                                                                                 
2                                                                                                 
3                                                                                                 
4                                                                                                 
5                                                                                                 
6                                                                                                 
7                                                                                                 
8                                                                                                 
9                                                                                                 
153                                                                                                 
370                                                                                                 
371                                                                                                 
407 

Flowchart:

Flowchart: Find all the narcissistic numbers between 1 and 1000


For more Practice: Solve these Related Problems:

  • Write a Java program to generate narcissistic numbers in a specified range using recursive digit extraction.
  • Write a Java program to verify the narcissistic property by comparing a number with the sum of its digits raised to the power of its length.
  • Write a Java program to filter narcissistic numbers from an array using Java streams.
  • Write a Java program to optimize narcissistic number detection by precomputing digit powers for 0–9.

Go to:


PREV : Check Mersenne Number.
NEXT : Check Palindrome Number.


Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.