Narcissistic Number
A narcissistic number (also known as a Armstrong number or a pluperfect digital invariant) is a number that is the sum of its own digits each raised to the power of the number of digits in the number itself. In other words, a narcissistic number is an n-digit number that is equal to the sum of its digits raised to the nth power.
For example, 153 is a narcissistic number because it has three digits and 1^3 + 5^3 + 3^3 = 153. Another example is 9474, which has four digits and 9^4 + 4^4 + 7^4 + 4^4 = 9474.
Some other examples of narcissistic numbers are:
- 1 (1^1 = 1)
- 2 (2^1 = 2)
- 3 (3^1 = 3)
- 4 (4^1 = 4)
- 5 (5^1 = 5)
- 6 (6^1 = 6)
- 7 (7^1 = 7)
- 8 (8^1 = 8)
- 9 (9^1 = 9)
- 1634 (1^4 + 6^4 + 3^4 + 4^4 = 1634)
- 8208 (8^4 + 2^4 + 0^4 + 8^4 = 8208)
- 9474 (9^4 + 4^4 + 7^4 + 4^4 = 9474)
Note that there are only finitely many narcissistic numbers, and they get increasingly rare as the number of digits increases. The largest known narcissistic number has 39 digits and is equal to 115,132,219,018,763,992,565,095,597,973,971,522,401.
Test 1 15351 is narcissistic number?
Yes, 15351 is a narcissistic number.
To check if a number is narcissistic or not, we need to compute the sum of its digits each raised to the power of the number of digits in the number itself, and compare the result with the original number.
In the case of 15351, it has 5 digits. Therefore, we need to compute:
1^5 + 5^5 + 3^5 + 5^5 + 1^5 = 1 + 3125 + 243 + 3125 + 1 = 6515
Since 15351 is equal to 6515, it is a narcissistic number.
Test 2 13232 is narcissistic numbers?
No, 13232 is not a narcissistic number.
To check if a number is narcissistic or not, we need to compute the sum of its digits each raised to the power of the number of digits in the number itself, and compare the result with the original number.
In the case of 13232, it has 5 digits. Therefore, we need to compute:
1^5 + 3^5 + 2^5 + 3^5 + 2^5 = 1 + 243 + 32 + 243 + 32 = 551
Since 13232 is not equal to 551, it is not a narcissistic number.
Here given code implementation process.
//C Program
//Check if a given number is Narcissistic number or not
#include <stdio.h>
#include <math.h>
//Count the number of digits in number
int digit_length(int number)
{
int size = 0;
while (number!=0)
{
number/=10;
size++;
}
return size;
}
int is_narcissistic(int number)
{
//Assign the value of how many number of digits are exist in number
int size = digit_length(number);
int auxiliary = number;
//Variable which control execution process
int result = 0;
int digit = 0;
while (auxiliary!=0) {
//Get last digit
digit = auxiliary % 10;
//remove last digit
auxiliary = auxiliary / 10;
//Add power of digit of number size
result = (pow(digit, size )) + result;
}
return result;
}
//Function which are handling the request of number is narcissistic number or not
void narcissistic(int number)
{
//Check the given number each digit power (length of number)
//Sum is equal to given number or not?
if(is_narcissistic(number)==number)
{
//When Yes
printf("%d Is an Narcissistic Number\n",number);
}
else
{
//When No
printf("%d Is not a Narcissistic Number\n",number);
}
}
int main() {
//Test Cases
narcissistic(153);
narcissistic(12);
narcissistic(345);
narcissistic(371);
return 0;
}
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
/*
C++ Program
Check if a given number is Narcissistic number or not
*/
#include<iostream>
#include <math.h>
using namespace std;
class MyNumber {
public:
//Count the number of digits in number
int digit_length(int number) {
int size = 0;
while (number != 0) {
number /= 10;
size++;
}
return size;
}
int is_narcissistic(int number) {
//Assign the value of how many number of digits are exist in number
int size = this->digit_length(number);
int auxiliary = number;
//Variable which control execution process
int result = 0;
int digit = 0;
while (auxiliary != 0) {
//Get last digit
digit = auxiliary % 10;
//remove last digit
auxiliary = auxiliary / 10;
//Add power of digit of number size
result = int(pow(digit, size)) + result;
}
return result;
}
//Function which are handling the request of number is narcissistic number or not
void narcissistic(int number) {
//Check the given number each digit power (length of number)
//Sum is equal to given number or not?
if (this->is_narcissistic(number) == number) {
//When Yes
cout << number << " Is an Narcissistic Number\n";
} else {
//When No
cout << number << " Is not a Narcissistic Number\n";
}
}
};
int main() {
MyNumber obj ;
//Test Cases
obj.narcissistic(153);
obj.narcissistic(12);
obj.narcissistic(345);
obj.narcissistic(371);
return 0;
}
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
/*
Java Program
Check if a given number is Narcissistic number or not
*/
public class MyNumber {
//Count the number of digits in number
public int digit_length(int number)
{
int size = 0;
while (number!=0)
{
number/=10;
size++;
}
return size;
}
public int is_narcissistic(int number)
{
//Assign the value of how many number of digits are exist in number
int size = digit_length(number);
int auxiliary = number;
//Variable which control execution process
int result = 0;
int digit = 0;
while (auxiliary!=0) {
//Get last digit
digit = auxiliary % 10;
//remove last digit
auxiliary = auxiliary / 10;
//Add power of digit of number size
result = (int)(Math.pow(digit, size )) + result;
}
return result;
}
//Function which are handling the request of number is narcissistic number or not
public void narcissistic(int number)
{
//Check the given number each digit power (length of number)
//Sum is equal to given number or not?
if(is_narcissistic(number)==number)
{
//When Yes
System.out.print(number+" Is an Narcissistic Number\n");
}
else
{
//When No
System.out.print(number+" Is not a Narcissistic Number\n");
}
}
public static void main(String[] args) {
MyNumber obj = new MyNumber();
//Test Cases
obj.narcissistic(153);
obj.narcissistic(12);
obj.narcissistic(345);
obj.narcissistic(371);
}
}
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
/*
C# Program
Check if a given number is Narcissistic number or not
*/
using System;
public class MyNumber {
//Count the number of digits in number
public int digit_length(int number) {
int size = 0;
while (number != 0) {
number /= 10;
size++;
}
return size;
}
public int is_narcissistic(int number) {
//Assign the value of how many number of digits are exist in number
int size = digit_length(number);
int auxiliary = number;
//Variable which control execution process
int result = 0;
int digit = 0;
while (auxiliary != 0) {
//Get last digit
digit = auxiliary % 10;
//remove last digit
auxiliary = auxiliary / 10;
//Add power of digit of number size
result = (int)(Math.Pow(digit, size)) + result;
}
return result;
}
//Function which are handling the request of number is narcissistic number or not
public void narcissistic(int number) {
//Check the given number each digit power .Length of number)
//Sum is equal to given number or not?
if (is_narcissistic(number) == number) {
//When Yes
Console.Write(number + " Is an Narcissistic Number\n");
} else {
//When No
Console.Write(number + " Is not a Narcissistic Number\n");
}
}
public static void Main(String[] args) {
MyNumber obj = new MyNumber();
//Test Cases
obj.narcissistic(153);
obj.narcissistic(12);
obj.narcissistic(345);
obj.narcissistic(371);
}
}
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
# Python 3 Program
# Check if a given number is Narcissistic number or not
class MyNumber :
# Count the number of digits in number
def digit_length(self, number) :
size = 0
while (number != 0) :
number = int(number / 10)
size += 1
return size
def is_narcissistic(self, number) :
# Assign the value of how many number of digits are exist in number
size = self.digit_length(number)
auxiliary = number
# Variable which control execution process
result = 0
digit = 0
while (auxiliary != 0) :
# Get last digit
digit = auxiliary % 10
# remove last digit
auxiliary = int(auxiliary / 10)
# Add power of digit of number size
result = (digit**size) + result
return result
# Function which are handling the request of number is narcissistic number or not
def narcissistic(self, number) :
# Check the given number each digit power (length of number)
# Sum is equal to given number or not?
if (self.is_narcissistic(number) == number) :
# When Yes
print(number ," Is an Narcissistic Number")
else :
# When No
print(number ," Is not a Narcissistic Number")
def main() :
obj = MyNumber()
# Test Cases
obj.narcissistic(153)
obj.narcissistic(12)
obj.narcissistic(345)
obj.narcissistic(371)
if __name__ == "__main__":
main()
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
# Ruby Program
# Check if a given number is Narcissistic number or not
class MyNumber
# Count the number of digits in number
def digit_length(number)
size = 0
while (number != 0)
number /= 10
size += 1
end
return size
end
def is_narcissistic(number)
# Assign the value of how many number of digits are exist in number
size = self.digit_length(number)
auxiliary = number
# Variable which control execution process
result = 0
digit = 0
while (auxiliary != 0)
# Get last digit
digit = auxiliary % 10
# remove last digit
auxiliary = auxiliary / 10
# Add power of digit of number size
result = (digit**size) + result
end
return result
end
# Function which are handling the request of number is narcissistic number or not
def narcissistic(number)
# Check the given number each digit power (length of number)
# Sum is equal to given number or not?
if (self.is_narcissistic(number) == number)
# When Yes
print(number ," Is an Narcissistic Number\n")
else
# When No
print(number ," Is not a Narcissistic Number\n")
end
end
end
def main()
obj = MyNumber.new()
# Test Cases
obj.narcissistic(153)
obj.narcissistic(12)
obj.narcissistic(345)
obj.narcissistic(371)
end
main()
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
/*
Scala Program
Check if a given number is Narcissistic number or not
*/
class MyNumber {
//Count the number of digits in number
def digit_length(value: Int): Int = {
var size: Int = 0;
var number: Int = value;
while (number != 0) {
number /= 10;
size += 1;
}
return size;
}
def is_narcissistic(number: Int): Int = {
//Assign the value of how many number of digits are exist in number
var size: Int = this.digit_length(number);
var auxiliary: Int = number;
//Variable which control execution process
var result: Int = 0;
var digit: Int = 0;
while (auxiliary != 0) {
//Get last digit
digit = auxiliary % 10;
//remove last digit
auxiliary = auxiliary / 10;
//Add power of digit of number size
result = (scala.math.pow(digit, size)).toInt + result;
}
return result;
}
//Function which are handling the request of number is narcissistic number or not
def narcissistic(number: Int): Unit = {
//Check the given number each digit power (length of number)
//Sum is equal to given number or not?
if (this.is_narcissistic(number) == number) {
//When Yes
print(s"$number Is an Narcissistic Number\n");
} else {
//When No
print(s"$number Is not a Narcissistic Number\n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Cases
obj.narcissistic(153);
obj.narcissistic(12);
obj.narcissistic(345);
obj.narcissistic(371);
}
}
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
/*
Swift 4 Program
Check if a given number is Narcissistic number or not
*/
import Foundation
class MyNumber {
//Count the number of digits in number
func digit_length(_ value: Int) -> Int {
var size: Int = 0;
var number: Int = value;
while (number != 0) {
number /= 10;
size += 1;
}
return size;
}
func is_narcissistic(_ number: Int) -> Int {
//Assign the value of how many number of digits are exist in number
let size: Int = self.digit_length(number);
var auxiliary: Int = number;
//Variable which control execution process
var result: Int = 0;
var digit: Int = 0;
while (auxiliary != 0) {
//Get last digit
digit = auxiliary % 10;
//remove last digit
auxiliary = auxiliary / 10;
//Add power of digit of number size
result = Int(pow(Double(digit), Double(size))) + result;
}
return result;
}
//Function which are handling the request of number is narcissistic number or not
func narcissistic(_ number: Int) {
//Check the given number each digit power (length of number)
//Sum is equal to given number or not?
if (self.is_narcissistic(number) == number) {
//When Yes
print(number ," Is an Narcissistic Number");
} else {
//When No
print(number ," Is not a Narcissistic Number");
}
}
}
func main() {
let obj: MyNumber = MyNumber();
//Test Cases
obj.narcissistic(153);
obj.narcissistic(12);
obj.narcissistic(345);
obj.narcissistic(371);
}
main();
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
<?php
/*
Php Program
Check if a given number is Narcissistic number or not
*/
class MyNumber {
//Count the number of digits in number
public function digit_length($number) {
$size = 0;
while ($number != 0) {
$number = intval($number/ 10);
$size++;
}
return $size;
}
public function is_narcissistic($number) {
//Assign the value of how many number of digits are exist in number
$size = $this->digit_length($number);
$auxiliary = $number;
//Variable which control execution process
$result = 0;
$digit = 0;
while ($auxiliary != 0) {
//Get last digit
$digit = $auxiliary % 10;
//remove last digit
$auxiliary = intval($auxiliary/ 10);
//Add power of digit of number size
$result = (pow($digit, $size)) + $result;
}
return $result;
}
//Function which are handling the request of number is narcissistic number or not
public function narcissistic($number) {
//Check the given number each digit power (length of number)
//Sum is equal to given number or not?
if ($this->is_narcissistic($number) == $number) {
//When Yes
echo($number ." Is an Narcissistic Number\n");
} else {
//When No
echo($number ." Is not a Narcissistic Number\n");
}
}
};
function main() {
$obj = new MyNumber();
//Test Cases
$obj->narcissistic(153);
$obj->narcissistic(12);
$obj->narcissistic(345);
$obj->narcissistic(371);
}
main();
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
/*
Node Js Program
Check if a given number is Narcissistic number or not
*/
class MyNumber {
//Count the number of digits in number
digit_length(number) {
var size = 0;
while (number != 0) {
number = parseInt(number / 10);
size++;
}
return size;
}
is_narcissistic(number) {
//Assign the value of how many number of digits are exist in number
var size = this.digit_length(number);
var auxiliary = number;
//Variable which control execution process
var result = 0;
var digit = 0;
while (auxiliary != 0) {
//Get last digit
digit = auxiliary % 10;
//remove last digit
auxiliary = parseInt(auxiliary / 10);
//Add power of digit of number size
result = parseInt(Math.pow(digit, size)) + result;
}
return result;
}
//Function which are handling the request of number is narcissistic number or not
narcissistic(number) {
//Check the given number each digit power (length of number)
//Sum is equal to given number or not?
if (this.is_narcissistic(number) == number) {
//When Yes
process.stdout.write(number + " Is an Narcissistic Number\n");
} else {
//When No
process.stdout.write(number + " Is not a Narcissistic Number\n");
}
}
}
function main(args) {
var obj = new MyNumber();
//Test Cases
obj.narcissistic(153);
obj.narcissistic(12);
obj.narcissistic(345);
obj.narcissistic(371)
}
main();
Output
153 Is an Narcissistic Number
12 Is not a Narcissistic Number
345 Is not a Narcissistic Number
371 Is an Narcissistic Number
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment