Menu
Topics Index
...
`

Find whether given number is Narcissistic number


If you need explanation Read this topic If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to find whether given number is Narcissistic number. Narcissistic number is a number that is the sum of its own digits each raised to the power of the number of digits equal to number itself.

Input (int) Output (boolean)
153 153 = 1^3 + 5^3 + 3^3
true
145 false
370 true
523 false
6 true



class IsNarcissisticNumber
{
    public static void main(String s[])
    {
        int number = 153;
        System.out.println("Is " + number + " a Narcissistic number : " + isNarcissisticNumber(number));

    }


    public static boolean isNarcissisticNumber(int number) {
    }

}
Topic: Java for loops vs Java while loops vs Java do while loops Java for loops vs Java while loops vs Java do while loops

If you need explanation Read this topic If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

here we need to find the length of the given num so first make that number into the string than find the length of that string and store that value in 1 variable
s=number+"";
*now store that number into temp variable
* take a variables sum and initialized with 0
* start while loop repeat until temp reaches 0
* in the loop perform % on the on temp and store res in temp1
* now call the Math.pow(temp1,length); add this res to the sum
* divide the sum by 10
after coming out of the loop compare number with the sum if both are equal than return true else return false

Posted by Uday Kumar    2015-02-11 08:55:45


To find whether the number is narcissistic or not we have to first find no of digits if a no.

---First we store number in temp variable
Int temp=number

--to find no of digits of no run while loop till number !=0 and inside the loop write logic to reverse a number.

---while(no!=0)
{
   Rem=no%2;
   No=no%2;
   Count =count +1;
}
---after that count will be having no of digits of a number. After that we have to find each digit raise to no of digits.

---Here, Math.pow() is used to find power of a number as follows:

To access each digit of no we convert no into string and use charAt() method to access each character and converting it into integer using integer.parseInt() method.

For(int I=0;I<count;I++)
{
Sum+=(int)Math.pow(integer.parseint(s.charAt(0)+"",count);
}

Check if sum is equlas to temp then return true else false.

Posted by Mânïshå Mùlchåndânï    2015-02-11 13:24:16


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-12 06:00:55


What about second correct and Merit Campus

Posted by Mânïshå Mùlchåndânï    2015-02-12 13:51:26


© meritcampus 2019

All Rights Reserved.

Open In App