SITE SEARCH

The factorial in Pascal: how to calculate. Sample Programs

Programming training is on the way fromsimple to complex. Having mastered data types and language operators, they pass to cyclic constructs. There are innumerable tasks for cycles: from the output of numbers to the column to the calculation of sums on complex formulas. Nevertheless, the beginning programmers still have a question: "How to calculate the factorial in Pascal?"

factorial in pascal

You can implement the task in at least three ways. They differ by the operators used.

Mathematical information

Before proceeding to the construction of algorithms andwriting programs, should study the theory. In mathematics, the factorial is the product of the integer for which the expression is calculated, by a positive integer less than it.

An example will help to understand the definition. Let it be required to find the factorial for number 3. Solution: 3! = 3 * 2 * 1 = 6.

The action of the exclamation mark, which is placed after the number, is indicated. An important note: the factorial is defined only for positive integers. At the same time, concepts for zero are introduced: 0! = 1.

factorial finding

To read the expression for large values ​​manually is a long occupation. To speed up the process of computing, use computer programs. Next, we discuss ways to find the factorial in Pascal.

The first way

The code below shows the version of the program.

program in pascal

The example uses a compound construction with a condition that is written before the body of the loop. Syntax of the record:

while {condition} do {operator_sequence};

The code is executed as follows: the program checks the validity of the expression {condition}, in the case of a positive check, passes to {operator_sequence}.

Returning to the program, you need to pay attention to the following lines:

  • 2 - the number is given n, for which the calculation will be performed;
  • 6 - the title of the cycle;
  • 7 - beginning of the cycle;
  • 8 - calculation of a variable fact, which stores the value of the factorial of a number n;
  • 9 - increase in the counter variable by one;
  • 10 - end of the cycle.

The second way

The following proposes to calculate the factorial in "Pascal" with the help of the operator repeat.

factorial in pascal program

Construction cycle: repeat {operator_sequence} until {condition};

To understand how the program works, consider it line by line:

  • 2 - constant n the number for which the calculation is performed is assigned;
  • 7 - beginning of the cycle;
  • 8, 9 - calculation of factorial and increase in the counter i;
  • 10 - end of the body of the cycle;
  • 11 - condition check, because the condition is placed after the sequence of operators, the action will be repeated at least once.

The third way

The latter program also makes it possible to calculate the factorial in "Pascal" and is the most compact in size. Cause - Operator Used for, for which the increase in the counter i is specified in the cycle parameters.

factorial in pascal program

Operator record: for {initial_value} to {final_value} do {operator_sequence}.

The code works as follows (the numbers indicate the lines of the listing):

  • 2 - constant n Assign the value of the number for which the factorial is calculated;
  • 6 - the cycle parameters are set - initial and final values;
  • 7 - beginning of the cycle;
  • 8 - calculation of a variable fact;
  • 9 - end of the cycle.

Comment

Even for numbers from the first ten, the factorial has a value greater than the data type allows integer. Therefore, the program in "Pascal" will show an error message. To fix it simply - you need to replace the data type for the result variable by longint or use types to store real values.

</ p>
  • Rating: