Narcissistic number

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 : 153
Output : yes
Explanation: 1^3+5^3+3^3=153 

Input : 1634
Output : yes
Explanation: 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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program for checking of
// Narcissistic number
#include <bits/stdc++.h>
using namespace std;
  
// 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) {
        sum += pow(dup % 10, l);
        dup /= 10;
    }
  
    return (n == sum);
}
  
// Driver code to check the 
// above function
int main()
{
    int n = 1634;
    if (check(n))
        cout << "yes";
    else
        cout << "no";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for checking
// of Narcissistic number
import java.io.*;
import static java.lang.Math.*;
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
boolean 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 code to check
// the above function
public 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program for checking of
# Narcissistic number
  
# function to count digits
def countDigit(n) :
    if (n == 0) :
        return 0
  
    return (1 + countDigit(n // 10))
      
  
# Returns true if n is Narcissistic number
def 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 code 
n = 1634
if (check(n)) :
    print( "yes")
else :
    print( "no"
      
      
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for checking
// of Narcissistic number
using 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program for checking of 
// Narcissistic number 
  
// Function to count digits 
function countDigit($n
    if ($n == 0) 
        return 0; 
  
    return (1 + countDigit($n / 10)); 
  
// Returns true if n is
// Narcissistic number 
function 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
?>

chevron_right



Output :

yes

References: http://mathandmultimedia.com/2012/01/16/narcissistic-numbers/



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t