w3resource

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

Java Numbers: Exercise-23 with Solution

Write a Java program to find all the 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

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program to check if a number is Mersenne number or not.
Next: Write a Java program to check if a number is palindrome or not.

What is the difficulty level of this exercise?



Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH