Narcissistic number

30 Apr, 2021
Given N, check whether it is a Narcissistic number or not.Note:Narcissistic Number is a number that is the sum of its own digits each raised to the power of the number of digits
Examples :  
Input : 153Output : yesExplanation: 1^3+5^3+3^3=153 
Input : 1634Output : yesExplanation: 1^4+6^4+3^4+4^4=1634 
The approach will be to count the number of digits and then extract every digit and then by using pow function we can get the power of that digit and then sum it up at the end and compare with the original number to check if it is a Narcissistic Number or not.
Below is the implementation of the above idea.
C++
Java
Python3
C#
PHP
Javascript
// CPP program for checking of// Narcissistic number#include <bits/stdc++.h>using namespace std; // function to count digitsint countDigit(int n){    if (n == 0)        return 0;     return 1 + countDigit(n / 10);} // Returns true if n is Narcissistic numberbool check(int n){    // count the number of digits    int l = countDigit(n);    int dup = n;    int sum = 0;     // calculates the sum of digits    // raised to power    while (dup)    {        sum += pow(dup % 10, l);        dup /= 10;    }     return (n == sum);} // Driver codeint main(){    int n = 1634;    if (check(n))        cout << "yes";    else        cout << "no";    return 0;}
// Java program for checking// of Narcissistic numberimport java.io.*;import static java.lang.Math.*;class narcissistic{ // function to count digitsint countDigit(int n){    if (n == 0)        return 0;      return 1 + countDigit(n / 10);}  // Returns true if n is Narcissistic numberboolean check(int n){    // count the number of digits    int l = countDigit(n);    int dup = n;    int sum = 0;      // calculates the sum of    //digits raised to power    while(dup > 0)    {        sum += pow(dup % 10, l);        dup /= 10;    }      return (n == sum);}  // Driver codepublic static void main(String args[]) {    narcissistic obj = new narcissistic();    int n = 1634;    if (obj.check(n))        System.out.println("yes");    else        System.out.println("no");  }} //This code is contributed by Anshika Goyal.
# Python 3 program for checking of# Narcissistic number # function to count digitsdef countDigit(n) :    if (n == 0) :        return 0     return (1 + countDigit(n // 10))      # Returns true if n is Narcissistic numberdef check(n) :         # Count the number of digits    l = countDigit(n)    dup = n; sm = 0     # Calculates the sum of digits    # raised to power    while (dup) :        sm = sm + pow(dup % 10, l)        dup = dup // 10         return (n == sm)      # Driver coden = 1634if (check(n)) :    print( "yes")else :    print( "no")           # This code is contributed by Nikita Tiwari.
// C# program for checking// of Narcissistic numberusing System; class narcissistic{     // function to count digits    int countDigit(int n)    {        if (n == 0)            return 0;             return 1 + countDigit(n / 10);    }         // Returns true if n is Narcissistic number    bool check(int n)    {        // count the number of digits        int l = countDigit(n);        int dup = n;        int sum = 0;             // calculates the sum of        //digits raised to power        while(dup > 0)        {            sum += (int)Math.Pow(dup % 10, l);            dup /= 10;        }             return (n == sum);    }         // Driver code    public static void Main()    {        narcissistic obj = new narcissistic();        int n = 1634;        if (obj.check(n))            Console.WriteLine("yes");        else            Console.WriteLine("no");    }} // This code is contributed by vt_m.
<?php// PHP program for checking of// Narcissistic number // Function to count digitsfunction countDigit($n){    if ($n == 0)        return 0;     return (1 + countDigit($n / 10));} // Returns true if n is// Narcissistic numberfunction check( $n){    // count the number of digits    $l = countDigit($n);    $dup = $n;    $sum = 0;     // calculates the sum of digits    // raised to power    while ($dup)    {        $sum += pow($dup % 10, $l);        $dup = (int)$dup / 10;    }     return ($n == $sum);} // Driver Code$n = 1634;if (check(!$n))    echo "yes";else    echo "no"; // This code is contributed by akt_mit?>
<script> // Javascript program for checking of// Narcissistic number  // Function to count digitsfunction countDigit(n){    if (n == 0)        return 0;      return (1 + countDigit(n / 10));}  // Returns true if n is// Narcissistic numberfunction check( n){    // count the number of digits    let l = countDigit(n);    let dup = n;    let sum = 0;      // calculates the sum of digits    // raised to power    while (dup)    {        sum += Math.pow(dup % 10, l);        dup = parseINT(dup / 10);    }      return (n == sum);}  // Driver Codelet n = 1634;if (check(!n))    document.write("yes");else    document.write("no");  // This code is contributed by _saurabh_jaiswal</script>
  
yes
 
Method 2: Simplified Method using string
 
We have to take input as a string and traverse through string computing power of each character with the length of the string. Note: Here length of the string gives the number of digits of that number
 
Below is the implementation of the above approach
 
C++14
Java
Python3
C#
Javascript
// CPP program for checking of// Narcissistic number#include <bits/stdc++.h>#include <string.h>using namespace std; string getResult(string st){    int sum = 0;    int length = st.length();         // Traversing through the string    for (int i = 0; i < length; i++)    {        // Since ascii value of numbers        // starts from 48 so we subtract it from sum        sum = sum + pow(st[i] - '0', length);    }         // Converting string to integer    int number = stoi(st);         // Comparing number and sum    if (number == sum)        return "yes";    else        return "no";} // Driver Codeint main(){    string st = "153";    cout << getResult(st);    return 0;}
// Java program for checking of// Narcissistic numberclass GFG{ static String getResult(String st){    int sum = 0;    int length = st.length();         // Traversing through the string    for (int i = 0; i < length; i++)    {               // Since ascii value of numbers        // starts from 48 so we subtract it from sum        sum = sum + (int)Math.pow(st.charAt(i) - '0', length);    }         // Converting string to integer    int number = Integer.parseInt(st);         // Comparing number and sum    if (number == sum)        return "yes";    else        return "no";} // Driver Codepublic static void main(String []args){    String st = "153";    System.out.print(getResult(st));}} // This code is contributed by rutvik_56.
# Python program for checking of# Narcissistic number  def getResult(st):    sum = 0    length = len(st)     # Traversing through the string    for i in st:         # Converting character to int        sum = sum + int(i) ** length     # Converting string to integer    number = int(st)     # Comparing number and sum    if (number == sum):        return "true"    else:        return "false"  # Driver Code# taking input as stringst = "153"print(getResult(st))
// C# program for checking of// Narcissistic numberusing System;class GFG{ static string getResult(string st){    int sum = 0;    int length = st.Length;         // Traversing through the string    for (int i = 0; i < length; i++)    {               // Since ascii value of numbers        // starts from 48 so we subtract it from sum        sum = sum + (int)Math.Pow(st[i] - '0', length);    }         // Converting string to integer    int number = int.Parse(st);         // Comparing number and sum    if (number == sum)        return "yes";    else        return "no";} // Driver Codepublic static void Main(string []args){    string st = "153";    Console.Write(getResult(st));}} // This code is contributed by pratham76.
<script>    // Javascript program for checking of    // Narcissistic number         function getResult(st)    {        let sum = 0;        let length = st.length;         // Traversing through the string        for (let i = 0; i < length; i++)        {             // Since ascii value of numbers            // starts from 48 so we subtract it from sum            sum = sum + Math.pow(st[i] - '0', length);        }         // Converting string to integer        let number = parseInt(st, 10);         // Comparing number and sum        if (number == sum)            return "yes";        else            return "no";    }         let st = "153";    document.write(getResult(st));     </script>
  
yes
 
 References: http://mathandmultimedia.com/2012/01/16/narcissistic-numbers/ 
 
jit_t
vikkycirus
rutvik_56
pratham76
_saurabh_jaiswal
mukesh07
number-digits
number-theory
Mathematical
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Minimum number of jumps to reach end
Find minimum number of coins that make a given value
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Program for Decimal to Binary Conversion