Factorial in Pascal: how to calculate. Program Examples

Programming training goes from simple to complex. Having mastered the data types and language operators, they move on to cyclic constructions. There are countless tasks for cycles: from the output of numbers in a column to the calculation of amounts using complex formulas. Nevertheless, novice programmers have the question: "How to calculate the factorial in Pascal?"

factorial in pascal

There are at least three ways to accomplish a task. They differ in the operators used.

Mathematical Information

Before proceeding to the construction of algorithms and writing programs, you should study the theory. In mathematics, the factorial is the product of an integer for which an expression is calculated, positive integers less than it.

An example helps to understand the definition. Suppose you want to find the factorial for the number 3. Solution: 3! = 3 * 2 * 1 = 6.

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

finding factorial

Reading the expression for large values โ€‹โ€‹manually is a long task. To speed up the process of computing, use computer programs. The following are ways to find the factorial in Pascal.

First way

The code below shows the program option.

program in pascal

In the example, a composite construction is used with a condition that is written before the body of the loop. Record Syntax:

while {condition} do {sequence of statements};

The code is executed as follows: the program checks the truth of the expression {condition} , in the case of a positive check it goes to the {sequence of statements } .

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

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

Second way

The following suggests calculating the factorial in Pascal using the repeat operator.

factorial in pascal program

Loop construction: repeat {sequence of statements } until {condition};

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

  • 2 - the constant n is assigned the number for which the calculation is performed;
  • 7 - beginning of the cycle;
  • 8, 9 - calculation of factorial and increase in counter i ;
  • 10 - end of the cycle body;
  • 11 - checking the condition, since the condition is located after the sequence of statements, the repetition of actions will be performed at least once.

Third way

The latter program also makes it possible to calculate the factorial in Pascal and is the most compact in size. The reason is the for statement used, for which the counter increment i is set in the loop parameters.

factorial in pascal program

Operator entry: for {start_value} to {end_value} do {sequence of statements }.

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

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

Comment

Even for numbers from the top ten, factorial matters more than the integer data type allows. Therefore, the program in Pascal will display an error message. To fix it is simple - you need to replace the data type for the result variable with longint or use the types to store real values.

Source: https://habr.com/ru/post/K23752/


All Articles