ComputersProgramming

The factorial in Pascal: how to calculate. Sample Programs

Programming training goes from simple 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?"

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 and writing programs, you 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.

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.

The example uses a composite 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, it switches to {operator_sequence} .

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

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

The second way

The following proposes to calculate the factorial in Pascal using the repeat statement.

The loop construction: repeat {operator_sequence} 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 - calculating the factorial and increasing the counter i ;
  • 10 - the 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 last program also makes it possible to calculate the factorial in "Pascal" and is the most compact in size. The reason is the used for statement, for which the increment of the counter i is specified in the loop parameters.

The statement of the operator: for {initial_value} to {final_value} do {sequence_of operators }.

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

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

Comment

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

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.birmiss.com. Theme powered by WordPress.