Krishnamurthy number program

Sum of factorial of all digits an number equal to original number is called Krishnamurthy Number.

Here given code implementation process.

// C program
// krishnamurthy number 
#include <stdio.h>

// Check whether given number is krishnamurthy number or not
void isKrishnamurthyNo(int number)
{
	if (number < 1)
	{
		return;
	}
	// Define some auxiliary variable
	int i = 1;
	int remainder = 0;
	int sum = 0;
	// Auxiliary space to store factorial
	int factorial[10];
	// First factorial
	factorial[0] = 1;
	// Calculate the factorial of range (1..9)
	for (i = 1; i <= 9; ++i)
	{
		factorial[i] = factorial[i - 1] *i;
	}
	i = number;
	// Calculate digit factorial sum of given number
	while (i != 0)
	{
		// Get the last digit
		remainder = i % 10;
		// Factorial sum
		sum += factorial[remainder];
		// Remove last digit
		i /= 10;
	}
	if (sum == number)
	{
		printf(" %d is krishnamurthy number \n", number);
	}
	else
	{
		printf(" %d is not krishnamurthy number \n", number);
	}
}
int main()
{
	// Test Case
	isKrishnamurthyNo(40585);
	isKrishnamurthyNo(65);
	isKrishnamurthyNo(145);
	return 0;
}

input

 40585 is krishnamurthy number
 65 is not krishnamurthy number
 145 is krishnamurthy number
/*
  Java Program for
  Krishnamurthy number 
*/
class FlaviusNumber
{
	// Auxiliary space to store factorial
	int[] factorial;
	public FlaviusNumber()
	{
		factorial = new int[10];
		this.findFactorial();
	}
	public void findFactorial()
	{
		int i = 0;
		// First factorial
		factorial[0] = 1;
		// Calculate the factorial of range (1..9)
		for (i = 1; i <= 9; ++i)
		{
			factorial[i] = factorial[i - 1] * i;
		}
	}
	// Check whether given number is krishnamurthy number or not
	public void isKrishnamurthyNo(int number)
	{
		if (number < 1)
		{
			return;
		}
		// Define some auxiliary variable
		int i = number;
		int remainder = 0;
		int sum = 0;
		// Calculate digit factorial sum of given number
		while (i != 0)
		{
			// Get the last digit
			remainder = i % 10;
			// Factorial sum
			sum += factorial[remainder];
			// Remove last digit
			i /= 10;
		}
		if (sum == number)
		{
          	// When result is krishnamurthy number
			System.out.println(" [" + number + "] is krishnamurthy number");
		}
		else
		{
			System.out.println(" [" + number + "] is not krishnamurthy number");
		}
	}
	public static void main(String[] args)
	{
		FlaviusNumber task = new FlaviusNumber();
		// Test case
		task.isKrishnamurthyNo(40585);
		task.isKrishnamurthyNo(65);
		task.isKrishnamurthyNo(145);
	}
}

input

 [40585] is krishnamurthy number
 [65] is not krishnamurthy number
 [145] is krishnamurthy number
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Krishnamurthy number 
*/
class FlaviusNumber
{
	public:
	// Auxiliary space to store factorial
	int *factorial;
	FlaviusNumber()
	{
		this->factorial = new int[10];
		this->findFactorial();
	}
	void findFactorial()
	{
		int i = 0;
		// First factorial
		this->factorial[0] = 1;
		// Calculate the factorial of range (1..9)
		for (i = 1; i <= 9; ++i)
		{
			this->factorial[i] = this->factorial[i - 1] * i;
		}
	}
	// Check whether given number is krishnamurthy number or not
	void isKrishnamurthyNo(int number)
	{
		if (number < 1)
		{
			return;
		}
		// Define some auxiliary variable
		int i = number;
		int remainder = 0;
		int sum = 0;
		// Calculate digit factorial sum of given number
		while (i != 0)
		{
			// Get the last digit
			remainder = i % 10;
			// Factorial sum
			sum += this->factorial[remainder];
			// Remove last digit
			i /= 10;
		}
		if (sum == number)
		{
			// When result is krishnamurthy number
			cout << " [" << number << "] is krishnamurthy number" << endl;
		}
		else
		{
			cout << " [" << number << "] is not krishnamurthy number" << endl;
		}
	}
};
int main()
{
	FlaviusNumber *task = new FlaviusNumber();
	// Test case
	task->isKrishnamurthyNo(40585);
	task->isKrishnamurthyNo(65);
	task->isKrishnamurthyNo(145);
	return 0;
}

input

 [40585] is krishnamurthy number
 [65] is not krishnamurthy number
 [145] is krishnamurthy number
// Include namespace system
using System;
/*
  Csharp Program for
  Krishnamurthy number 
*/
public class FlaviusNumber
{
	// Auxiliary space to store factorial
	int[] factorial;
	public FlaviusNumber()
	{
		this.factorial = new int[10];
		this.findFactorial();
	}
	public void findFactorial()
	{
		int i = 0;
		// First factorial
		this.factorial[0] = 1;
		// Calculate the factorial of range (1..9)
		for (i = 1; i <= 9; ++i)
		{
			this.factorial[i] = this.factorial[i - 1] * i;
		}
	}
	// Check whether given number is krishnamurthy number or not
	public void isKrishnamurthyNo(int number)
	{
		if (number < 1)
		{
			return;
		}
		// Define some auxiliary variable
		int i = number;
		int remainder = 0;
		int sum = 0;
		// Calculate digit factorial sum of given number
		while (i != 0)
		{
			// Get the last digit
			remainder = i % 10;
			// Factorial sum
			sum += this.factorial[remainder];
			// Remove last digit
			i /= 10;
		}
		if (sum == number)
		{
			// When result is krishnamurthy number
			Console.WriteLine(" [" + number + "] is krishnamurthy number");
		}
		else
		{
			Console.WriteLine(" [" + number + "] is not krishnamurthy number");
		}
	}
	public static void Main(String[] args)
	{
		FlaviusNumber task = new FlaviusNumber();
		// Test case
		task.isKrishnamurthyNo(40585);
		task.isKrishnamurthyNo(65);
		task.isKrishnamurthyNo(145);
	}
}

input

 [40585] is krishnamurthy number
 [65] is not krishnamurthy number
 [145] is krishnamurthy number
<?php
/*
  Php Program for
  Krishnamurthy number 
*/
class FlaviusNumber
{
	// Auxiliary space to store factorial
	public $factorial;
	public function __construct()
	{
		$this->factorial = array_fill(0, 10, 0);
		$this->findFactorial();
	}
	public function findFactorial()
	{
		$i = 0;
		// First factorial
		$this->factorial[0] = 1;
		// Calculate the factorial of range (1..9)
		for ($i = 1; $i <= 9; ++$i)
		{
			$this->factorial[$i] = $this->factorial[$i - 1] * $i;
		}
	}
	// Check whether given number is krishnamurthy number or not
	public	function isKrishnamurthyNo($number)
	{
		if ($number < 1)
		{
			return;
		}
		// Define some auxiliary variable
		$i = $number;
		$remainder = 0;
		$sum = 0;
		// Calculate digit factorial sum of given number
		while ($i != 0)
		{
			// Get the last digit
			$remainder = $i % 10;
			// Factorial sum
			$sum += $this->factorial[$remainder];
			// Remove last digit
			$i = (int)($i / 10);
		}
		if ($sum == $number)
		{
			// When result is krishnamurthy number
			echo " [".$number."] is krishnamurthy number"."\n";
		}
		else
		{
			echo " [".$number."] is not krishnamurthy number"."\n";
		}
	}
}

function main()
{
	$task = new FlaviusNumber();
	// Test case
	$task->isKrishnamurthyNo(40585);
	$task->isKrishnamurthyNo(65);
	$task->isKrishnamurthyNo(145);
}
main();

input

 [40585] is krishnamurthy number
 [65] is not krishnamurthy number
 [145] is krishnamurthy number
/*
  Node JS Program for
  Krishnamurthy number 
*/
class FlaviusNumber
{
	constructor()
	{
		this.factorial = Array(10).fill(0);
		this.findFactorial();
	}
	findFactorial()
	{
		var i = 0;
		// First factorial
		this.factorial[0] = 1;
		// Calculate the factorial of range (1..9)
		for (i = 1; i <= 9; ++i)
		{
			this.factorial[i] = this.factorial[i - 1] * i;
		}
	}
	// Check whether given number is krishnamurthy number or not
	isKrishnamurthyNo(number)
	{
		if (number < 1)
		{
			return;
		}
		// Define some auxiliary variable
		var i = number;
		var remainder = 0;
		var sum = 0;
		// Calculate digit factorial sum of given number
		while (i != 0)
		{
			// Get the last digit
			remainder = i % 10;
			// Factorial sum
			sum += this.factorial[remainder];
			// Remove last digit
			i = parseInt(i / 10);
		}
		if (sum == number)
		{
			// When result is krishnamurthy number
			console.log(" [" + number + "] is krishnamurthy number");
		}
		else
		{
			console.log(" [" + number + "] is not krishnamurthy number");
		}
	}
}

function main()
{
	var task = new FlaviusNumber();
	// Test case
	task.isKrishnamurthyNo(40585);
	task.isKrishnamurthyNo(65);
	task.isKrishnamurthyNo(145);
}
main();

input

 [40585] is krishnamurthy number
 [65] is not krishnamurthy number
 [145] is krishnamurthy number
#  Python 3 Program for
#  Krishnamurthy number 
class FlaviusNumber :

	def __init__(self) :
		self.factorial = [0] * (10)
		self.findFactorial()
	
	def findFactorial(self) :
		i = 1
		#  First factorial
		self.factorial[0] = 1
		#  Calculate the factorial of range (1..9)
		while (i <= 9) :
			self.factorial[i] = self.factorial[i - 1] * i
			i += 1
		
	
	#  Check whether given number is krishnamurthy number or not
	def isKrishnamurthyNo(self, number) :
		if (number < 1) :
			return
		
		i = number
		remainder = 0
		sum = 0
		#  Calculate digit factorial sum of given number
		while (i != 0) :
			#  Get the last digit
			remainder = i % 10
			#  Factorial sum
			sum += self.factorial[remainder]
			#  Remove last digit
			i = int(i / 10)
		
		if (sum == number) :
			#  When result is krishnamurthy number
			print(" [", number ,"] is krishnamurthy number")
		else :
			print(" [", number ,"] is not krishnamurthy number")
		
	

def main() :
	task = FlaviusNumber()
	#  Test case
	task.isKrishnamurthyNo(40585)
	task.isKrishnamurthyNo(65)
	task.isKrishnamurthyNo(145)

if __name__ == "__main__": main()

input

 [ 40585 ] is krishnamurthy number
 [ 65 ] is not krishnamurthy number
 [ 145 ] is krishnamurthy number
#  Ruby Program for
#  Krishnamurthy number 
class FlaviusNumber 
	# Define the accessor and reader of class FlaviusNumber
	attr_reader :factorial
	attr_accessor :factorial
	#  Auxiliary space to store factorial
	def initialize() 
		self.factorial = Array.new(10) {0}
		self.findFactorial()
	end

	def findFactorial() 
		i = 0
		#  First factorial
		self.factorial[0] = 1
		#  Calculate the factorial of range (1..9)
		i = 1
		while (i <= 9) 
			self.factorial[i] = self.factorial[i - 1] * i
			i += 1
		end

	end

	#  Check whether given number is krishnamurthy number or not
	def isKrishnamurthyNo(number) 
		if (number < 1) 
			return
		end

		#  Define some auxiliary variable
		i = number
		remainder = 0
		sum = 0
		#  Calculate digit factorial sum of given number
		while (i != 0) 
			#  Get the last digit
			remainder = i % 10
			#  Factorial sum
			sum += self.factorial[remainder]
			#  Remove last digit
			i = i / 10
		end

		if (sum == number) 
			#  When result is krishnamurthy number
			print(" [", number ,"] is krishnamurthy number", "\n")
		else 
			print(" [", number ,"] is not krishnamurthy number", "\n")
		end

	end

end

def main() 
	task = FlaviusNumber.new()
	#  Test case
	task.isKrishnamurthyNo(40585)
	task.isKrishnamurthyNo(65)
	task.isKrishnamurthyNo(145)
end

main()

input

 [40585] is krishnamurthy number
 [65] is not krishnamurthy number
 [145] is krishnamurthy number
/*
  Scala Program for
  Krishnamurthy number 
*/
class FlaviusNumber(var factorial: Array[Int])
{
	def this()
	{
		this(Array.fill[Int](10)(0));
      	this.findFactorial();
	}
	def findFactorial(): Unit = {
		var i: Int = 0;
		// First factorial
		factorial(0) = 1;
		// Calculate the factorial of range (1..9)
		i = 1;
		while (i <= 9)
		{
			factorial(i) = factorial(i - 1) * i;
			i += 1;
		}
	}
	// Check whether given number is krishnamurthy number or not
	def isKrishnamurthyNo(number: Int): Unit = {
		if (number < 1)
		{
			return;
		}
		// Define some auxiliary variable
		var i: Int = number;
		var remainder: Int = 0;
		var sum: Int = 0;
		// Calculate digit factorial sum of given number
		while (i != 0)
		{
			// Get the last digit
			remainder = i % 10;
			// Factorial sum
			sum += factorial(remainder);
			// Remove last digit
			i = (i / 10).toInt;
		}
		if (sum == number)
		{
			// When result is krishnamurthy number
			println(" [" + number + "] is krishnamurthy number");
		}
		else
		{
			println(" [" + number + "] is not krishnamurthy number");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: FlaviusNumber = new FlaviusNumber();
		// Test case
		task.isKrishnamurthyNo(40585);
		task.isKrishnamurthyNo(65);
		task.isKrishnamurthyNo(145);
	}
}

input

 [40585] is krishnamurthy number
 [65] is not krishnamurthy number
 [145] is krishnamurthy number
/*
  Swift 4 Program for
  Krishnamurthy number 
*/
class FlaviusNumber
{
	// Auxiliary space to store factorial
	var factorial: [Int];
	init()
	{
		self.factorial = Array(repeating: 0, count: 10);
		self.findFactorial();
	}
	func findFactorial()
	{
		var i: Int = 0;
		// First factorial
		self.factorial[0] = 1;
		// Calculate the factorial of range (1..9)
		i = 1;
		while (i <= 9)
		{
			self.factorial[i] = self.factorial[i - 1] * i;
			i += 1;
		}
	}
	// Check whether given number is krishnamurthy number or not
	func isKrishnamurthyNo(_ number: Int)
	{
		if (number < 1)
		{
			return;
		}
		// Define some auxiliary variable
		var i: Int = number;
		var remainder: Int = 0;
		var sum: Int = 0;
		// Calculate digit factorial sum of given number
		while (i  != 0)
		{
			// Get the last digit
			remainder = i % 10;
			// Factorial sum
			sum += self.factorial[remainder];
			// Remove last digit
			i = i / 10;
		}
		if (sum == number)
		{
			// When result is krishnamurthy number
			print(" [", number ,"] is krishnamurthy number");
		}
		else
		{
			print(" [", number ,"] is not krishnamurthy number");
		}
	}
}
func main()
{
	let task: FlaviusNumber = FlaviusNumber();
	// Test case
	task.isKrishnamurthyNo(40585);
	task.isKrishnamurthyNo(65);
	task.isKrishnamurthyNo(145);
}
main();

input

 [ 40585 ] is krishnamurthy number
 [ 65 ] is not krishnamurthy number
 [ 145 ] is krishnamurthy number
/*
  Kotlin Program for
  Krishnamurthy number 
*/
class FlaviusNumber
{
	// Auxiliary space to store factorial
	var factorial: Array < Int > ;
	
	constructor()
	{
		this.factorial = Array(10)
		{
			0
		};
		this.findFactorial();
	}
	fun findFactorial(): Unit
	{
		var i: Int = 1;
		// First factorial
		this.factorial[0] = 1;
		while (i <= 9)
		{
			this.factorial[i] = this.factorial[i - 1] * i;
			i += 1;
		}
	}
	// Check whether given number is krishnamurthy number or not
	fun isKrishnamurthyNo(number: Int): Unit
	{
		if (number < 1)
		{
			return;
		}
		// Define some auxiliary variable
		var i: Int = number;
		var remainder: Int ;
		var sum: Int = 0;
		while (i != 0)
		{
			// Get the last digit
			remainder = i % 10;
			// Factorial sum
			sum += this.factorial[remainder];
			// Remove last digit
			i = i / 10;
		}
		if (sum == number)
		{
			// When result is krishnamurthy number
			println(" [" + number + "] is krishnamurthy number");
		}
		else
		{
			println(" [" + number + "] is not krishnamurthy number");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: FlaviusNumber = FlaviusNumber();
	// Test case
	task.isKrishnamurthyNo(40585);
	task.isKrishnamurthyNo(65);
	task.isKrishnamurthyNo(145);
}

input

 [40585] is krishnamurthy number
 [65] is not krishnamurthy number
 [145] is krishnamurthy 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