Find sum of digits in factorial of a number

Here given code implementation process.

import java.util.Vector;
/*
    Java program
    Find sum of digits in factorial of a number
*/
public class Factorial
{
	public void multiply(Vector < Integer > record, int v)
	{
		int carry = 0;
		int product = 0;
		for (int i = 0; i < record.size(); ++i)
		{
			product = record.get(i) * v + carry;
			// Update value
			record.set(i, product % 10);
			carry = product / 10;
		}
		// When carry not zero 
		while (carry > 0)
		{
			// Add new record element
			record.add(carry % 10);
			// Remove last digit in carry
			carry = carry / 10;
		}
	}
	public void findFactorial(int n)
	{
		if (n < 0)
		{
			return;
		}
		Vector < Integer > record = new Vector < Integer > ();
		int digitSum = 0;
		// First element
		record.add(1);
		// Find factorial
		for (int v = 2; v <= n; ++v)
		{
			multiply(record, v);
		}
		System.out.println("\nGiven n   : " + n);
		System.out.print("Factorial : ");
		// Display calculated value 
		for (int i = record.size() - 1; i >= 0; --i)
		{
			// Sum of digits
			digitSum += record.get(i);
			System.out.print(record.get(i));
		}
		System.out.print("\nDigit Sum : " + digitSum);
	}
	public static void main(String[] args)
	{
		Factorial task = new Factorial();
		// Test
		task.findFactorial(25);
		task.findFactorial(65);
	}
}

Output

Given n   : 25
Factorial : 15511210043330985984000000
Digit Sum : 72
Given n   : 65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum : 351
// Include header file
#include <iostream>
#include <vector>

using namespace std;
/*
    C++ program
    Find sum of digits in factorial of a number
*/
class Factorial
{
	public: void multiply(vector < int > &record, int v)
	{
		int carry = 0;
		int product = 0;
		for (int i = 0; i < record.size(); ++i)
		{
			product = record.at(i) *v + carry;
			// Update value
			record.at(i) = product % 10;
			carry = product / 10;
		}
		// When carry not zero 
		while (carry > 0)
		{
			// Add new record element
			record.push_back(carry % 10);
			// Remove last digit in carry
			carry = carry / 10;
		}
	}
	void findFactorial(int n)
	{
		if (n < 0)
		{
			return;
		}
		vector < int > record;
		int digitSum = 0;
		// First element
		record.push_back(1);
		// Find factorial
		for (int v = 2; v <= n; ++v)
		{
			this->multiply(record, v);
		}
		cout << "\nGiven n   : " << n << endl;
		cout << "Factorial : ";
		// Display calculated value 
		for (int i = record.size() - 1; i >= 0; --i)
		{
			// Sum of digits
			digitSum += record.at(i);
			cout << record.at(i);
		}
		cout << "\nDigit Sum : " << digitSum;
	}
};
int main()
{
	Factorial *task = new Factorial();
	// Test
	task->findFactorial(25);
	task->findFactorial(65);
	return 0;
}

Output

Given n   : 25
Factorial : 15511210043330985984000000
Digit Sum : 72
Given n   : 65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum : 351
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp program
    Find sum of digits in factorial of a number
*/
public class Factorial
{
	public void multiply(List < int > record, int v)
	{
		int carry = 0;
		int product = 0;
		for (int i = 0; i < record.Count; ++i)
		{
			product = record[i] * v + carry;
			// Update value
			record[i] = product % 10;
			carry = product / 10;
		}
		// When carry not zero 
		while (carry > 0)
		{
			// Add new record element
			record.Add(carry % 10);
			// Remove last digit in carry
			carry = carry / 10;
		}
	}
	public void findFactorial(int n)
	{
		if (n < 0)
		{
			return;
		}
		List < int > record = new List < int > ();
		int digitSum = 0;
		// First element
		record.Add(1);
		// Find factorial
		for (int v = 2; v <= n; ++v)
		{
			this.multiply(record, v);
		}
		Console.WriteLine("\nGiven n   : " + n);
		Console.Write("Factorial : ");
		// Display calculated value 
		for (int i = record.Count - 1; i >= 0; --i)
		{
			// Sum of digits
			digitSum += record[i];
			Console.Write(record[i]);
		}
		Console.Write("\nDigit Sum : " + digitSum);
	}
	public static void Main(String[] args)
	{
		Factorial task = new Factorial();
		// Test
		task.findFactorial(25);
		task.findFactorial(65);
	}
}

Output

Given n   : 25
Factorial : 15511210043330985984000000
Digit Sum : 72
Given n   : 65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum : 351
package main
import "fmt"
/*
    Go program
    Find sum of digits in factorial of a number
*/
type Factorial struct {}
func getFactorial() * Factorial {
	var me *Factorial = &Factorial {}
	return me
}
func(this Factorial) multiply(record*[]int, v int) {
	var carry int = 0
	var product int = 0
	for i := 0 ; i < len(*record) ; i++ {
		product = (*record)[i] * v + carry
		// Update value
		(*record)[i] = product % 10
		carry = product / 10
	}
	// When carry not zero 
	for (carry > 0) {
		// Add new record element
		(*record) = append((*record), carry % 10)
		// Remove last digit in carry
		carry = carry / 10
	}
}
func(this Factorial) findFactorial(n int) {
	if n < 0 {
		return
	}
	var record = make([]int,0)
	var digitSum int = 0
	// First element
	record = append(record, 1)
	// Find factorial
	for v := 2 ; v <= n ; v++ {
		this.multiply(&record, v)
	}
	fmt.Println("\nGiven n   : ", n)
	fmt.Print("Factorial : ")
	// Display calculated value 
	for i := len(record) - 1 ; i >= 0 ; i-- {
		// Sum of digits
		digitSum += record[i]
		fmt.Print(record[i])
	}
	fmt.Print("\nDigit Sum : ", digitSum)
}
func main() {
	var task * Factorial = getFactorial()
	// Test
	task.findFactorial(25)
	task.findFactorial(65)
}

Output

Given n   : 25
Factorial : 15511210043330985984000000
Digit Sum : 72
Given n   : 65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum : 351
<?php
/*
    Php program
    Find sum of digits in factorial of a number
*/
class Factorial
{
	public	function multiply(&$record, $v)
	{
		$carry = 0;
		$product = 0;
		for ($i = 0; $i < count($record); ++$i)
		{
			$product = $record[$i] * $v + $carry;
			// Update value
			$record[$i] = $product % 10;
			$carry = (int)($product / 10);
		}
		// When carry not zero 
		while ($carry > 0)
		{
			// Add new record element
			$record[] = $carry % 10;
			// Remove last digit in carry
			$carry = (int)($carry / 10);
		}
	}
	public	function findFactorial($n)
	{
		if ($n < 0)
		{
			return;
		}
		$record = array();
		$digitSum = 0;
		// First element
		$record[] = 1;
		// Find factorial
		for ($v = 2; $v <= $n; ++$v)
		{
			$this->multiply($record, $v);
		}
		echo("\nGiven n   : ".$n."\n");
		echo("Factorial : ");
		// Display calculated value 
		for ($i = count($record) - 1; $i >= 0; --$i)
		{
			// Sum of digits
			$digitSum += $record[$i];
			echo($record[$i]);
		}
		echo("\nDigit Sum : ".$digitSum);
	}
}

function main()
{
	$task = new Factorial();
	// Test
	$task->findFactorial(25);
	$task->findFactorial(65);
}
main();

Output

Given n   : 25
Factorial : 15511210043330985984000000
Digit Sum : 72
Given n   : 65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum : 351
/*
    Node JS program
    Find sum of digits in factorial of a number
*/
class Factorial
{
	multiply(record, v)
	{
		var carry = 0;
		var product = 0;
		for (var i = 0; i < record.length; ++i)
		{
			product = record[i] * v + carry;
			// Update value
			record[i] = product % 10;
			carry = parseInt(product / 10);
		}
		// When carry not zero 
		while (carry > 0)
		{
			// Add new record element
			record.push(carry % 10);
			// Remove last digit in carry
			carry = parseInt(carry / 10);
		}
	}
	findFactorial(n)
	{
		if (n < 0)
		{
			return;
		}
		var record = [];
		var digitSum = 0;
		// First element
		record.push(1);
		// Find factorial
		for (var v = 2; v <= n; ++v)
		{
			this.multiply(record, v);
		}
		console.log("\nGiven n   : " + n);
		process.stdout.write("Factorial : ");
		// Display calculated value 
		for (var i = record.length - 1; i >= 0; --i)
		{
			// Sum of digits
			digitSum += record[i];
			process.stdout.write(""+record[i]);
		}
		process.stdout.write("\nDigit Sum : " + digitSum);
	}
}

function main()
{
	var task = new Factorial();
	// Test
	task.findFactorial(25);
	task.findFactorial(65);
}
main();

Output

Given n   : 25
Factorial : 15511210043330985984000000
Digit Sum : 72
Given n   : 65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum : 351
#    Python 3 program
#    Find sum of digits in factorial of a number
class Factorial :
	def multiply(self, record, v) :
		carry = 0
		product = 0
		i = 0
		while (i < len(record)) :
			product = record[i] * v + carry
			#  Update value
			record[i] = product % 10
			carry = int(product / 10)
			i += 1
		
		#  When carry not zero 
		while (carry > 0) :
			#  Add new record element
			record.append(carry % 10)
			#  Remove last digit in carry
			carry = int(carry / 10)
		
	
	def findFactorial(self, n) :
		if (n < 0) :
			return
		
		record = []
		digitSum = 0
		#  First element
		record.append(1)
		v = 2
		#  Find factorial
		while (v <= n) :
			self.multiply(record, v)
			v += 1
		
		print("\nGiven n   : ", n)
		print("Factorial : ", end = "")
		i = len(record) - 1
		#  Display calculated value 
		while (i >= 0) :
			#  Sum of digits
			digitSum += record[i]
			print(record[i], end = "")
			i -= 1
		
		print("\nDigit Sum : ", digitSum, end = "")
	

def main() :
	task = Factorial()
	#  Test
	task.findFactorial(25)
	task.findFactorial(65)

if __name__ == "__main__": main()

Output

Given n   :  25
Factorial : 15511210043330985984000000
Digit Sum :  72
Given n   :  65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum :  351
#    Ruby program
#    Find sum of digits in factorial of a number
class Factorial 
	def multiply(record, v) 
		carry = 0
		product = 0
		i = 0
		while (i < record.length) 
			product = record[i] * v + carry
			#  Update value
			record[i] = product % 10
			carry = product / 10
			i += 1
		end

		#  When carry not zero 
		while (carry > 0) 
			#  Add new record element
			record.push(carry % 10)
			#  Remove last digit in carry
			carry = carry / 10
		end

	end

	def findFactorial(n) 
		if (n < 0) 
			return
		end

		record = []
		digitSum = 0
		#  First element
		record.push(1)
		v = 2
		#  Find factorial
		while (v <= n) 
			self.multiply(record, v)
			v += 1
		end

		print("\nGiven n   : ", n, "\n")
		print("Factorial : ")
		i = record.length - 1
		#  Display calculated value 
		while (i >= 0) 
			#  Sum of digits
			digitSum += record[i]
			print(record[i])
			i -= 1
		end

		print("\nDigit Sum : ", digitSum)
	end

end

def main() 
	task = Factorial.new()
	#  Test
	task.findFactorial(25)
	task.findFactorial(65)
end

main()

Output

Given n   : 25
Factorial : 15511210043330985984000000
Digit Sum : 72
Given n   : 65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum : 351
import scala.collection.mutable._;
/*
    Scala program
    Find sum of digits in factorial of a number
*/
class Factorial()
{
	def multiply(record: ArrayBuffer[Int], v: Int): Unit = {
		var carry: Int = 0;
		var product: Int = 0;
		var i: Int = 0;
		while (i < record.size)
		{
			product = record(i) * v + carry;
			// Update value
			record(i) = product % 10;
			carry = product / 10;
			i += 1;
		}
		// When carry not zero 
		while (carry > 0)
		{
			// Add new record element
			record += carry % 10;
			// Remove last digit in carry
			carry = carry / 10;
		}
	}
	def findFactorial(n: Int): Unit = {
		if (n < 0)
		{
			return;
		}
		var record: ArrayBuffer[Int] = new ArrayBuffer[Int]();
		var digitSum: Int = 0;
		// First element
		record += 1;
		var v: Int = 2;
		// Find factorial
		while (v <= n)
		{
			multiply(record, v);
			v += 1;
		}
		println("\nGiven n   : " + n);
		print("Factorial : ");
		var i: Int = record.size - 1;
		// Display calculated value 
		while (i >= 0)
		{
			// Sum of digits
			digitSum += record(i);
			print(record(i));
			i -= 1;
		}
		print("\nDigit Sum : " + digitSum);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Factorial = new Factorial();
		// Test
		task.findFactorial(25);
		task.findFactorial(65);
	}
}

Output

Given n   : 25
Factorial : 15511210043330985984000000
Digit Sum : 72
Given n   : 65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum : 351
import Foundation;
/*
    Swift 4 program
    Find sum of digits in factorial of a number
*/
class Factorial
{
	func multiply(_ record: inout[Int], _ v: Int)
	{
		var carry: Int = 0;
		var product: Int = 0;
		var i: Int = 0;
		while (i < record.count)
		{
			product = record[i] * v + carry;
			// Update value
			record[i] = product % 10;
			carry = product / 10;
			i += 1;
		}
		// When carry not zero 
		while (carry > 0)
		{
			// Add new record element
			record.append(carry % 10);
			// Remove last digit in carry
			carry = carry / 10;
		}
	}
	func findFactorial(_ n: Int)
	{
		if (n < 0)
		{
			return;
		}
		var record: [Int] = [Int]();
		var digitSum: Int = 0;
		// First element
		record.append(1);
		var v: Int = 2;
		// Find factorial
		while (v <= n)
		{
			self.multiply(&record, v);
			v += 1;
		}
		print("\nGiven n   : ", n);
		print("Factorial : ", terminator: "");
		var i: Int = record.count - 1;
		// Display calculated value 
		while (i >= 0)
		{
			// Sum of digits
			digitSum += record[i];
			print(record[i], terminator: "");
			i -= 1;
		}
		print("\nDigit Sum : ", digitSum, terminator: "");
	}
}
func main()
{
	let task: Factorial = Factorial();
	// Test
	task.findFactorial(25);
	task.findFactorial(65);
}
main();

Output

Given n   :  25
Factorial : 15511210043330985984000000
Digit Sum :  72
Given n   :  65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum :  351
/*
    Kotlin program
    Find sum of digits in factorial of a number
*/
class Factorial
{
	fun multiply(record: MutableList < Int >  , v : Int): Unit
	{
		var carry: Int = 0;
		var product: Int;
		var i: Int = 0;
		while (i < record.size)
		{
			product = record[i] * v + carry;
			// Update value
			record[i] = product % 10;
			carry = product / 10;
			i += 1;
		}
		// When carry not zero 
		while (carry > 0)
		{
			// Add new record element
			record.add(carry % 10);
			// Remove last digit in carry
			carry = carry / 10;
		}
	}
	fun findFactorial(n: Int): Unit
	{
		if (n < 0)
		{
			return;
		}
		val record: MutableList < Int > = mutableListOf < Int > ();
		var digitSum: Int = 0;
		// First element
		record.add(1);
		var v: Int = 2;
		// Find factorial
		while (v <= n)
		{
			this.multiply(record, v);
			v += 1;
		}
		println("\nGiven n   : " + n);
		print("Factorial : ");
		var i: Int = record.size - 1;
		// Display calculated value 
		while (i >= 0)
		{
			// Sum of digits
			digitSum += record[i];
			print(record[i]);
			i -= 1;
		}
		print("\nDigit Sum : " + digitSum);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Factorial = Factorial();
	// Test
	task.findFactorial(25);
	task.findFactorial(65);
}

Output

Given n   : 25
Factorial : 15511210043330985984000000
Digit Sum : 72
Given n   : 65
Factorial : 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
Digit Sum : 351


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