Pseudocode Examples

Pseudocode for Calculating Factorial and Fibonacci Sequence

In computer science and mathematics, two classic problems often introduced to beginners are calculating the factorial of a number and generating the Fibonacci sequence.
Understanding the pseudocode for these problems helps build a strong foundation for learning recursion, loops, and algorithmic thinking.

This article explains both factorial and Fibonacci calculations through simple and clear pseudocode examples.


What is a Factorial?




The factorial of a number n (written as n!) is the product of all positive integers less than or equal to n.

Example:
5! = 5 × 4 × 3 × 2 × 1 = 120

Factorials are widely used in combinatorics, probability, and algorithm design.


Pseudocode for Calculating Factorial (Using Loop)


Pseudocode for Calculating Factorial (Using Recursion)


What is the Fibonacci Sequence?

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1.

Example:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...

Fibonacci numbers appear in mathematics, nature (such as flower petals), and even computer algorithms.


Pseudocode for Calculating Fibonacci Sequence (Using Loop)


Pseudocode for Calculating Fibonacci (Using Recursion)


Factorial vs Fibonacci: Key Differences

AspectFactorialFibonacci Sequence
DefinitionProduct of integers up to nSum of two preceding numbers
Common UseCombinatorics, PermutationsNature patterns, Algorithm design
Base Cases0! = 1, 1! = 1F(0) = 0, F(1) = 1
Growth RateVery fast (n!)Moderate (exponential)

Conclusion

Learning to write pseudocode for factorial and Fibonacci problems is an excellent exercise for beginners in computer science.

  • Use loops when you need simple, fast calculations.
  • Use recursion when you want a more elegant or theoretical approach.

These fundamental problems also prepare you for advanced topics like dynamic programming, memoization, and algorithm optimization.

Leave a Comment