Factorial Loop Init Error

Our Objective

To implement a program that finds the factorial of a given number (with loop initialization error).

 

The Theory

The program takes an input integer "num" from the user and initializes two variables "fact" and "i" to 1 and 0 respectively. It then enters a "while" loop that continues to execute until "i" is greater than "num".

Within the loop, it multiplies the current value of "fact" by "i" and stores the result in "fact". It then increments the value of "i" by 1.

After the loop has completed, the program prints the final value of "fact". However, there is an error in this program because the initialization of "i" should be 1 instead of 0, otherwise the result of "fact" will always be 0, because 0 multiplied by any number is always 0.

 

Learning Outcomes 

  • Understanding of the concept of factorial: The program will require an understanding of what a factorial is, how it is calculated, and what it represents mathematically.
  • Programming skills: To write a program that calculates the factorial of a number, you will need to know how to code using a programming language such as Python, Java, or C++.
  • Logical thinking: You will need to think logically and break down the problem of calculating the factorial into smaller steps that the computer can follow.
  • Problem-solving skills: As with any programming task, you will need to be able to identify and solve any errors that may arise in the program.
  • Testing and debugging: You will need to test your program to ensure that it works correctly, and be able to debug any issues that arise during testing.
  • Familiarity with mathematical concepts and formulas: The program will involve working with mathematical concepts and formulas, so familiarity with these will be helpful.