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







© 2021, kalkicode.com, All rights reserved