Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to sum the digit of a factorial of a number in Pascal

1 Answer

0 votes
program FactorialAndSumDigits;

function SumDigits(num: LongInt): Integer;
var
  sum: Integer;
begin
  sum := 0;
  while num <> 0 do
  begin
    sum := sum + (num mod 10);
    num := num div 10;
  end;
  SumDigits := sum;
end;

function Factorial(n: LongInt): LongInt;
begin
  if (n = 1) or (n = 0) then
    Factorial := 1
  else
    Factorial := n * Factorial(n - 1);
end;

var
  number, result: LongInt;
begin
  number := 9;
  result := Factorial(number);

  WriteLn('factorial = ', result);
  WriteLn('sum digits = ', SumDigits(result));
end.



(*
run:

factorial = 362880
sum digits = 27

*)

 



answered Feb 11, 2025 by avibootz
...