Narcissistic numbers list – Python Program to Check a number is Narcissistic Number or Not

Narcissistic numbers list: Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Given a number, the task is to check whether the given number is a Narcissistic Number or Not in Python.

Narcissistic Number:

Narcissistic numbers are a type of number that can be generated by adding the sum of its own digits raised to the power of the number of digits.

Example:

370

Number of digits=3

3*(Number of digits)+7*(Number of digits)+0*(Number of digits)

3^(3)+7^(3)+0^3=370

So it is a Narcissistic Number.

Examples:

Example1:

Input:

Given Number =370

Output:

The given Number { 370 } is a Narcissistic Number

Example2:

Input:

Given Number =371

Output:

The given Number { 371 } is a Narcissistic Number

Program to Check a number is Narcissistic Number or Not in Python

Below are the ways to check whether the given number is a Narcissistic Number or Not in Python.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Convert this number into list of digits using list(),map(),int(),str() functions.
  • Store it in a variable.
  • Calculate the length of the list using the len() function and store it in a variable say listleng.
  • Take a variable tempo and initialize its value to 0.
  • Loop in this digits list using For loop.
  • Calculate the iterator value^listleng where ^ represents the power operator and store it in a variable.
  • Increment the tempo by the above variable.
  • After the end of For loop check if the tempo value is equal to the given number using the If conditional Statement.
  • If it is true then print the given number as a Narcissistic Number.
  • Else it is not a Narcissistic Number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
givennmb = 370
# Convert this number into list of digits using list(),map(),int(),str() functions.
# Store it in a variable.
numbedigis = list(map(int, str(givennmb)))
# Calculate the length of the list using the len() function
# and store it in a variable say listleng.
listleng = len(numbedigis)
# Take a variable tempo and initialize its value to 0.
tempo = 0
# Loop in this digits list using For loop.
for numbrdigit in numbedigis:
        # Calculate the iterator value^listleng where ^ represents
    # the power operator and store it in a variable.
    powevalu = numbrdigit**listleng
    # Increment the tempo by the above variable.
    tempo = tempo+powevalu
# After the end of For loop check if the tempo value is equal
# to the given number using the If conditional Statement.
if(tempo == givennmb):
        # If it is true then print the given number as a Narcissistic Number.
    print('The given Number {', givennmb, '} is a Narcissistic Number')
else:
        # Else it is not a Narcissistic Number.
    print('The given Number {', givennmb, '} is not a Narcissistic Number')

Output:

The given Number { 370 } is a Narcissistic Number

Method #2: Using For Loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Convert this number into list of digits using list(),map(),int(),str() functions.
  • Store it in a variable.
  • Calculate the length of the list using the len() function and store it in a variable say listleng.
  • Take a variable tempo and initialize its value to 0.
  • Loop in this digits list using For loop.
  • Calculate the iterator value^listleng where ^ represents the power operator and store it in a variable.
  • Increment the tempo by the above variable.
  • After the end of For loop check if the tempo value is equal to the given number using the If conditional Statement.
  • If it is true then print the given number as a Narcissistic Number.
  • Else it is not a Narcissistic Number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
givennmb = int(input('Enter some random number = '))
# Convert this number into list of digits using list(),map(),int(),str() functions.
# Store it in a variable.
numbedigis = list(map(int, str(givennmb)))
# Calculate the length of the list using the len() function
# and store it in a variable say listleng.
listleng = len(numbedigis)
# Take a variable tempo and initialize its value to 0.
tempo = 0
# Loop in this digits list using For loop.
for numbrdigit in numbedigis:
        # Calculate the iterator value^listleng where ^ represents
    # the power operator and store it in a variable.
    powevalu = numbrdigit**listleng
    # Increment the tempo by the above variable.
    tempo = tempo+powevalu
# After the end of For loop check if the tempo value is equal
# to the given number using the If conditional Statement.
if(tempo == givennmb):
        # If it is true then print the given number as a Narcissistic Number.
    print('The given Number {', givennmb, '} is a Narcissistic Number')
else:
        # Else it is not a Narcissistic Number.
    print('The given Number {', givennmb, '} is not a Narcissistic Number')

Output:

Enter some random number = 371
The given Number { 371 } is a Narcissistic Number

Related Programs: