Header-C
Header-C

 

Introduction

A factorial is a mathematical function that multiplies a given number by every whole number less than it down to 1.
Factorial is commonly denoted by “n!” and is calculated as:

n! = n × (n-1) × (n-2) × … × 1

Factorials are widely used in fields such as algebra, probability, and combinatorics. In this program, we will calculate
the factorial of a given number using a simple iterative approach.

Objective

The objective of this program is to:

  • Understand how to calculate factorials in C programming.
  • Implement a program that asks the user for an input number and calculates its factorial.
  • Understand the usage of loops in C to perform iterative calculations.

Code


#include 

// Function to calculate factorial
long long int factorial(int n) {
    long long int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;  // Multiply the result by the current number
    }
    return result;
}

int main() {
    int num;
    printf("Enter a positive integer: ");
    scanf("%d", &num);  // Taking input from the user

    // Validate if the input is a positive integer
    if (num < 0) {
        printf("Error! Factorial of a negative number doesn't exist.\n");
    } else {
        // Call the factorial function and display the result
        printf("Factorial of %d is %lld\n", num, factorial(num));
    }
    return 0;
}
        

Explanation of the Program

This program calculates the factorial of a given number using an iterative approach. Let’s break it down step by step:

  1. Function Declaration: The program defines a function factorial that takes an integer n as its argument and returns a long long int representing the factorial.
  2. Iterative Loop: Inside the factorial function, a for loop runs from 1 to n. For each iteration, it multiplies the result by the current loop index i.
  3. User Input: In the main function, the program prompts the user to enter a positive integer. The input is read using scanf.
  4. Error Handling: If the user enters a negative number, the program displays an error message stating that factorial for negative numbers is not possible.
  5. Output: If the input is valid, the program calls the factorial function and prints the result in the format Factorial of is .

How to Run the Program

To run this program, follow these steps:

  1. Ensure you have a C compiler installed, such as GCC or Clang.
  2. Create a new C source file, e.g., factorial.c.
  3. Copy the provided C code into this file.
  4. Open a terminal or command prompt and navigate to the folder containing your factorial.c file.
  5. Compile the program using the command:
    gcc -o factorial factorial.c
  6. Run the program by typing:
    ./factorial
  7. The program will prompt you to enter a number, and it will display the factorial result.

Conclusion

This simple program demonstrates the calculation of a factorial using an iterative approach in C programming.
The program is an excellent starting point for understanding loops, user input, and basic error handling in C.
You can also modify the program to handle larger numbers or optimize it for performance using recursion if desired.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)