Calculate the factorial of a given number n (n> 0) () - that is, the product of numbers from 1 to n.

This is similar, but it does not work for me ...

Program my; var i, s: integer; f: longint; begin s := 1; for i := 1 to 20 do f := f*i; writeln('произведение от 1 до 20 ',f); readln; end. 
  • The answers were correct, good, but on one condition - not counting 20! But smaller. 20! in longint will not get. If possible, reduce the original data, if not, you need to edit the program. - alexlz

2 answers 2

Because, as I understand it, you initially wanted the factorial value to be contained in the variable "s", but later for some reason, you created the variable "f", which generally stores the address of the memory cell in which it is located (you did not null it ). Here is the function that returns factorial to "n":

 function factorial(n):integer; var i,f: integer; begin f:=1; for i := 1 to n do f := f*i; result:=f; end; 
  • Yes, I figured it out) it was recorded in the outline, it can be seen when I wrote it mixed up - dadayar

The only thing left is to assign the initial value to the variable f. Now it is not initialized, so the final value is zero.

 Program my; var i: integer; f: longint = 1; begin for i := 1 to 10 do f := f*i; writeln('произведение от 1 до 10 ', f); end. 
  • the variable is initialized, simply Assen assigned not at initialization, but before the cycle :) It is possible in 2 ways. Let the author of the question himself choose how he is more comfortable - Justinserg