×

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other terms, it consists of positive numbers with m digits and m th powers of digits. Other names for it include Armstrong number, Plus Perfect, and pluperfect. The sequence is A005188 from the OEIS. 

Let's use instances for a better understanding of the narcissistic number.

Examples:

153 = 13+ 53 + 33 = 1 + 125 +27 = 153

8208 = 84 + 24 + 04 + 84 = 4096 + 16 + 0 + 4096 = 8208

We can see that the supplied number is a power of the entire number of digits, and the combination of the values of each digit equals the number we have considered. Consequently, the numbers provided are narcissistic.

Some of the examples of Narcissistic Numbers: 1, 2,3,4,5,6,7,8,9,150,407…

Steps Required

Following the steps required for finding the Narcissistic Number:

N can be read or initialized.

To record the specified number N's digit counting in a variable power, measure its digits.

Calculate each digit's power before adding the results in a variable total.

Comparing the variable total to N, which is provided. The provided number is narcissistic. If both numbers are equal, it is not.

Java Program for Narcissistic Number

NarcissisticExample.java

//this program is for finding the given number is Narcissistic number 
//importing section
import java.util.*;  
import static java.lang.Math.*;  
public class NarcissisticExample 
{  
//function for determining the number of digits in a given number
int numDigits(int n)  
{  
if (n== 0)  
return 0;  
return 1 + numDigits(n/10);  
}  
//method for determining the given number is Narcissistic Number
boolean isNumber(int n)  
{  
// counting the digits of a given number  
int power = numDigits(n);  
//the given value is assigned to n 
int num = n;  
//result for storing the sum of the powers  
int total = 0;  
//the loop will iterate until the condition becomes false  
while(num> 0)  
{  
//it will find the last digit of the given number and 
//then, the power for the last digit is calculated 
//after the calculation of the power, it will store in the total variable  
total+= pow(num% 10, power);  
//and then the last digit of the number is then removed 
num= num / 10;  
}  
//the result(total) is then compared with the given number
return (n == total);  
}  
//main program
public static void main(String args[])  
{  
NarcissisticExample  nrs = new NarcissisticExample();  
Scanner sc = new Scanner(System.in);  
System.out.print("Please enter the input number: ");  
//reading the integer number from the user 
int n= sc.nextInt();  
//  
if (nrs.isNumber(n))  
System.out.println("The given input number "+n+" is a narcissistic number.");  
else  
System.out.println("The given input number "+n+" is not a narcissistic number.");  
}  
}  

Output

Narcissistic Number in Java

This program is for finding the Narcissistic numbers of the given range.

NarcissisticExample2.java

//this program is for finding the narcissistic numbers for a given range
//import section
import java.util.LinkedList;  
public class NarcissisticExample2  
{  
//Main section of the program 
public static void main(String args[])  
{  
for (int i = 1; i < 1000; i++)   
{  
int num = i;  
//a linked list was created for data storing 
LinkedList<Integer> d = new LinkedList<>();  
//the loop will iterate until the condition becomes false  
while (num > 0)   
{  
//the last of the given number is added to the list     
d.push( num % 10 );  
//after that, the last digit is removed 
num = num/ 10;  
}  
//the result is stored in the variable total
int total = 0;  
//the loop will continue over the list  
for(Integer number : d)   
{  
// finding the power of the digits    
total += Math.pow(number, d.size());  
}  
//the result(total) is then compared with the given number 
if(i == total)   
{  
//printing the range of numbers
System.out.println(i);  
}  
}  
}  
}   

Output

Narcissistic Number in Java

Related Topics

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

How to Convert String to Date in Java

How to Convert String to Date in java You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes....

2 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Static and Dynamic Binding in Java

Data Binding in Java The relation between a class and method or class and field is known as Data Binding. In Java, we can create a class for Student_info, but until we...

2 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.