# A Program That Calculates the Factorial of a Given Number in Algol

Algol (short for Algorithmic Language) is a high-level programming language that was developed in the late 1950s. It was one of the first languages designed for scientific and engineering computations. In this article, we will show you how to write a program in Algol that calculates the factorial of a given number.

Factorial is a mathematical operation that multiplies a number by all the numbers below it. For example, the factorial of 5 (written as 5!) is calculated as:

5! = 5 x 4 x 3 x 2 x 1 = 120

To calculate the factorial of a number using Algol, we will use a loop to multiply the number by all the numbers below it. Here is the algorithm:

1. Start with a variable n, which is the number whose factorial we want to calculate.
2. Initialize a variable called result to 1.
3. Use a loop to multiply result by all the numbers from 1 to n.
4. Return the result.

Now let’s write the program in Algol:

begin
integer n, result, i;

``````write("Enter a number: ");

result := 1;

for i := 1 step 1 until n do
result := result * i;

write("Factorial of ", n, " is ", result);``````

end.

In this program, we first declare three variables: n, result, and i. n is the input number whose factorial we want to calculate, result is the variable that will hold the final result, and i is a loop counter.

We then prompt the user to enter a number and read it into the variable n.

Next, we initialize the variable result to 1, as we will be multiplying it by all the numbers below n.

We then use a for loop to multiply result by all the numbers from 1 to n. The loop starts at 1 and increments i by 1 until i is equal to n.

Finally, we print out the result.

Let’s try running the program with n = 5:

Enter a number: 5
Factorial of 5 is 120

As you can see, the program correctly calculates the factorial of 5 to be 120.

In conclusion, writing a program to calculate the factorial of a number using Algol is a straightforward process. By using a loop to multiply the number by all the numbers below it, we can calculate the factorial of any number.

Algol is a language that is known for its simplicity and clarity. It was designed to be easy to read and write, making it a popular language for scientific and mathematical calculations. Algol has had a significant impact on the development of computer science, and many of its concepts are still used in modern programming languages.

When writing a program in Algol, it is important to remember that Algol uses a block structure. Each block consists of a sequence of statements enclosed in braces. Blocks can be nested, with the inner blocks being contained within the outer blocks.

Algol also uses a structured programming approach, which emphasizes the use of control structures such as loops and conditional statements. This makes Algol code easier to read and understand, as it follows a logical structure.

Here’s an example of a program in Algol that uses a conditional statement:

begin
integer age;

``````write("Enter your age: ");

if age >= 18 then
else
write("You are a minor.");
fi``````

end.

In this program, we declare an integer variable called age and prompt the user to enter their age. We then use an if statement to check if the age is greater than or equal to 18. If it is, we print “You are an adult.” If it is not, we print “You are a minor.”

Algol also supports functions, which are a set of statements that perform a specific task. Functions can be called from within other parts of the program, making them useful for organizing code and reducing repetition.

Here’s an example of a program in Algol that uses a function to calculate the sum of two numbers:

begin
integer x, y;

``````write("Enter the first number: ");

write("Enter the second number: ");

write("The sum is ", sum(x, y));``````

end.

function sum(a, b: integer) : integer;
begin
return a + b;
end.

In this program, we declare two integer variables called x and y, prompt the user to enter two numbers, and then call the sum function to calculate the sum of the two numbers. The function takes two integer arguments and returns their sum.

Overall, Algol is a powerful and flexible programming language that is well-suited for scientific and mathematical calculations. Its simple syntax and structured programming approach make it easy to learn and use, while its support for functions and control structures allows for the creation of complex programs.