#13 Calculating Factorial: A Simple C Program

Welcome to this blog post, where we will explore how to write a C program to calculate the factorial of a number. Factorials are fundamental in mathematics and often require efficient algorithms for precise calculations. By the end of this guide, you will have a solid understanding of factorial calculation and be able to implement it in your C programs. So, let's dive into the world of coding and discover how to compute factorials efficiently!

Understanding Factorials
Before we dive into the code, let's briefly review what factorials are. In mathematics, the factorial of a non-negative integer is the product of all positive integers less than or equal to that number. It is denoted by the exclamation mark (!) following the number. For example, the factorial of 5 is written as 5! and calculated as:

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

Writing the C Program
Let's write a simple C program that calculates the factorial of a given number. Here's the code:


Explaining the Code
Let's go through the code step by step to understand how it works:
  1. We start by including the <stdio.h> header file, which provides the necessary functions for input and output operations.
  2. The main() function is the entry point of the program. It is where the execution begins.
  3. Inside the main() function, we declare three variables: a, i, and fact.
    • 'a' is an integer variable that will store the user input.
    • 'i' is an integer variable that will be used as a loop counter.
    • 'fact' is a long long integer variable that will hold the factorial value.
  4. We use the printf() function to display the message "Enter number: " to prompt the user for input.
  5. The scanf() function is used to read the user input and store it in the variable a. The %d format specifier is used to indicate that we are expecting an integer input.
  6. We enter a for loop that starts from 1 and continues until i is less than or equal to a. This loop will iterate 'a' times.
  7. Inside the loop, we update the fact variable by multiplying it with the current value of i. This gradually calculates the factorial as we multiply each number from 1 to a.
  8. Once the loop finishes, we use the printf() function to display the result. We provide the format string "Factorial of %d is %ld." which contains placeholders for the values of a and fact. The %d format specifier is used for a (integer), and %ld is used for fact (long long integer).
  9. The program ends by returning 0 from the main() function, indicating successful execution.

Testing the Program
Now that we have the program ready, let's test it with some sample inputs. Here's an example:



The program successfully calculates the factorial of the given number and displays the result.

Conclusion
Congratulations! You have successfully learned how to write a C program to calculate the factorial of a number. By understanding the iterative approach and using the provided code as a reference, you can now implement factorial calculations in your C.

Post a Comment

0 Comments