×

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

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 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.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

Self-Descriptive Numbers in Java

A number n is given. Identifying the self-descriptive numbers that exist between 1 and n is our task. Self-Descriptive Numbers The definition of a self-descriptive number, m, is a number with b...

6 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Pernicious Number in Java

If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to...

4 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.