Count digits in a factorial

Here given code implementation process.

// C program
// Count digits in a factorial 
#include <stdio.h>
#include <math.h>

//Find digits of a factorial number
void factorial_digits(int number)
{
	int digit = 1;
	if (number < 0)
	{
		//When get negative number
		digit = 0;
	}
	else if (number > 1)
	{
		//Find digit
		double result = ((number * log10(number / M_E) + log10(2 * M_PI * number) / 2.0));
		digit = floor(result) + 1;
	}
	printf("\n [%d] Factorial digit : [%d]", number, digit);
}
int main()
{
	//Test case
	/*
	     Factorial of [5]
	     // 120
	     // Digit : 3
	 */
	factorial_digits(5);
	/*
	     Factorial of [12]
	     // 479,001,600
	     // Digit : 9
	 */
	factorial_digits(12);
	/*
	    Factorial of [15]
	    // 1307674368000
	    // Digit : 13
	*/
	factorial_digits(15);
	/*
	Factorial of [220]

	//  2.283860335 E+421

	2283860335914641457397265865
	115333727042973071546228701773
	634716126027692603024845877776
	549791921102945706558196074779
	575009550523224197049956176972
	302056587667226166060976323404
	977554732543013557133146825747
	553799450849523377065894531021
	055272516334278466875614904921
	365807833845853428557155180084
	957884822642989867003294551385
	992993862178352349027264696691
	854493614080000000000000000000
	000000000000000000000000000000
	0000
	// Digit : 422

	*/
	factorial_digits(220);
	return 0;
}

Output

 [5] Factorial digit : [3]
 [12] Factorial digit : [9]
 [15] Factorial digit : [13]
 [220] Factorial digit : [422]
// Java program 
// Count digits in a factorial 
class FactorialNo
{
	//Find digits of a factorial number
	public void factorial_digits(int number)
	{
		int digit = 1;
		if (number < 0)
		{
			//When get negative number
			digit = 0;
		}
		else if (number > 1)
		{
			//Find digit
			double result = ((number * Math.log10(number / Math.E) + Math.log10(2 * Math.PI * number) / 2.0));
			digit = (int)Math.floor(result) + 1;
		}
		System.out.print("\n [" + number + "] Factorial digit : [" + digit + "]");
	}
	public static void main(String[] args)
	{
		FactorialNo obj = new FactorialNo();
		//Test case
		obj.factorial_digits(5);
		obj.factorial_digits(12);
		obj.factorial_digits(15);
		obj.factorial_digits(220);
	}
}

Output

 [5] Factorial digit : [3]
 [12] Factorial digit : [9]
 [15] Factorial digit : [13]
 [220] Factorial digit : [422]
//Include header file
#include <iostream>
#include<math.h>
using namespace std;

// C++ program 
// Count digits in a factorial 

class FactorialNo
{
	public:
		//Find digits of a factorial number
		void factorial_digits(int number)
		{
			int digit = 1;
			if (number < 0)
			{
				//When get negative number
				digit = 0;
			}
			else if (number > 1)
			{
				//Find digit
				double result = ((number *log10(number / M_E) + log10(2  * M_PI * number) / 2.0));
				digit = (int) floor(result) + 1;
			}
			cout << "\n [" << number << "] Factorial digit : [" << digit << "]";
		}
};
int main()
{
	FactorialNo obj = FactorialNo();
	//Test case
	obj.factorial_digits(5);
	obj.factorial_digits(12);
	obj.factorial_digits(15);
	obj.factorial_digits(220);
	return 0;
}

Output

 [5] Factorial digit : [3]
 [12] Factorial digit : [9]
 [15] Factorial digit : [13]
 [220] Factorial digit : [422]
//Include namespace system
using System;
// C# program 
// Count digits in a factorial 
class FactorialNo
{
	//Find digits of a factorial number
	public void factorial_digits(int number)
	{
		int digit = 1;
		if (number < 0)
		{
			//When get negative number
			digit = 0;
		}
		else if (number > 1)
		{
			//Find digit
			double result = ((number * Math.Log10(number / Math.E) + Math.Log10(2 * Math.PI * number) / 2.0));
			digit = (int) Math.Floor(result) + 1;
		}
		Console.Write("\n [" + number + "] Factorial digit : [" + digit + "]");
	}
	public static void Main(String[] args)
	{
		FactorialNo obj = new FactorialNo();
		//Test case
		obj.factorial_digits(5);
		obj.factorial_digits(12);
		obj.factorial_digits(15);
		obj.factorial_digits(220);
	}
}

Output

 [5] Factorial digit : [3]
 [12] Factorial digit : [9]
 [15] Factorial digit : [13]
 [220] Factorial digit : [422]
<?php
// Php program 
// Count digits in a factorial 
class FactorialNo
{
	//Find digits of a factorial number
	public	function factorial_digits($number)
	{
		$digit = 1;
		if ($number < 0)
		{
			//When get negative number
			$digit = 0;
		}
		else if ($number > 1)
		{
			//Find digit
			$result = (($number * log10(intval($number / M_E)) + intval(log10(2 * M_PI * $number) / 2.0)));
			$digit = (floor($result)) + 1;
		}
		echo "\n [". $number ."] Factorial digit : [". $digit ."]";
	}
}

function main()
{
	$obj = new FactorialNo();
	//Test case
	$obj->factorial_digits(5);
	$obj->factorial_digits(12);
	$obj->factorial_digits(15);
	$obj->factorial_digits(220);
}
main();

Output

 [5] Factorial digit : [1]
 [12] Factorial digit : [8]
 [15] Factorial digit : [11]
 [220] Factorial digit : [420]
// Node Js program 
// Count digits in a factorial 
class FactorialNo
{
	//Find digits of a factorial number
	factorial_digits(number)
	{
		var digit = 1;
		if (number < 0)
		{
			//When get negative number
			digit = 0;
		}
		else if (number > 1)
		{
			//Find digit
			var result = ((number * Math.log10(parseInt(number / Math.E)) + parseInt(Math.log10(2 * Math.PI * number) / 2.0)));
			digit = (Math.floor(result)) + 1;
		}
		process.stdout.write("\n [" + number + "] Factorial digit : [" + digit + "]");
	}
}

function main()
{
	var obj = new FactorialNo();
	//Test case
	obj.factorial_digits(5);
	obj.factorial_digits(12);
	obj.factorial_digits(15);
	obj.factorial_digits(220);
}
main();

Output

 [5] Factorial digit : [1]
 [12] Factorial digit : [8]
 [15] Factorial digit : [11]
 [220] Factorial digit : [420]
import math
#  Python 3 program 
#  Count digits in a factorial 
class FactorialNo :
	# Find digits of a factorial number
	def factorial_digits(self, number) :
		digit = 1
		if (number < 0) :
			# When get negative number
			digit = 0
		
		elif(number > 1) :
			# Find digit
			result = ((number * math.log10(int(number / math.e)) + int(math.log10(2 * math.pi * number) / 2.0)))
			digit = (math.floor(result)) + 1
		
		print("\n [{0}] Factorial digit : [{1}]".format(number,digit), end = "")
	

def main() :
	obj = FactorialNo()
	# Test case
	obj.factorial_digits(5)
	obj.factorial_digits(12)
	obj.factorial_digits(15)
	obj.factorial_digits(220)

if __name__ == "__main__": main()

Output

 [5] Factorial digit : [1]
 [12] Factorial digit : [8]
 [15] Factorial digit : [11]
 [220] Factorial digit : [420]
#  Ruby program 
#  Count digits in a factorial 
class FactorialNo 
	# Find digits of a factorial number
	def factorial_digits(number) 
		digit = 1
		if (number < 0) 
			# When get negative number
			digit = 0
		elsif(number > 1) 
			# Find digit
			result = ((number * Math.log10(number / Math::E) + Math.log10(2 * Math::PI * number) / 2.0))
			digit = (result.floor()).to_i + 1
		end

		print("\n [", number ,"] Factorial digit : [", digit ,"]")
	end

end

def main() 
	obj = FactorialNo.new()
	# Test case
	obj.factorial_digits(5)
	obj.factorial_digits(12)
	obj.factorial_digits(15)
	obj.factorial_digits(220)
end

main()

Output

 [5] Factorial digit : [3]
 [12] Factorial digit : [9]
 [15] Factorial digit : [13]
 [220] Factorial digit : [422]
// Scala program 
// Count digits in a factorial 
class FactorialNo
{
	//Find digits of a factorial number
	def factorial_digits(number: Int): Unit = {
		var digit: Int = 1;
		if (number < 0)
		{
			//When get negative number
			digit = 0;
		}
		else if (number > 1)
		{
			//Find digit
			var result: Double = ((number * Math.log10((number / Math.E).toInt) + (Math.log10(2 * Math.PI * number) / 2.0).toInt));
			digit = (Math.floor(result)).toInt + 1;
		}
		print("\n [" + number + "] Factorial digit : [" + digit + "]");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: FactorialNo = new FactorialNo();
		//Test case
		obj.factorial_digits(5);
		obj.factorial_digits(12);
		obj.factorial_digits(15);
		obj.factorial_digits(220);
	}
}

Output

 [5] Factorial digit : [1]
 [12] Factorial digit : [8]
 [15] Factorial digit : [11]
 [220] Factorial digit : [420]
import Foundation
// Swift 4 program 
// Count digits in a factorial 
class FactorialNo
{
	//Find digits of a factorial number
	func factorial_digits(_ number: Int)
	{
		var digit: Int = 1;
		if (number < 0)
		{
			//When get negative number
			digit = 0;
		}
		else if (number > 1)
		{
			//Find digit
			let result: Double = ((Double(number) * log10(Double(number) / M_E) + log10(2 * Double.pi * Double(number)) / 2.0));
			digit = Int(floor(result)) + 1;
	}
	print("\n [\(number)] Factorial digit : [\(digit)] ", terminator: "");
}
}
func main()
{
	let obj: FactorialNo = FactorialNo();
	//Test case
	obj.factorial_digits(5);
	obj.factorial_digits(12);
	obj.factorial_digits(15);
	obj.factorial_digits(220);
}
main();

Output

 [5] Factorial digit : [3]
 [12] Factorial digit : [9]
 [15] Factorial digit : [13]
 [220] Factorial digit : [422]


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