C Program to Find the Factorial of a Number

Introduction

The factorial of a non-negative integer ( n ) is the product of all positive integers less than or equal to ( n ). Factorials are commonly used in permutations, combinations, and other mathematical calculations. This guide will show you how to create a C program to calculate the factorial of a number provided by the user.

Problem Statement

Create a C program that:

  • Takes an integer input from the user.
  • Calculates the factorial of that number.
  • Displays the factorial to the user.

Example:

  • Input: 5
  • Output: 120 (Factorial of 5 is ( 5 \times 4 \times 3 \times 2 \times 1 = 120 ))

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the number and the factorial result.
  4. Input the Number: Use scanf to take input from the user for the number.
  5. Calculate the Factorial: Use a loop to multiply the integers from 1 to the input number.
  6. Display the Result: Use printf to display the factorial.

C Program

#include <stdio.h>

/**
 * C Program to Find the Factorial of a Number
 * Author: https://www.javaguides.net/
 */
int main() {
    // Step 1: Declare variables to hold the number and factorial result
    int num;
    unsigned long long factorial = 1;  // Factorial can grow large, so use unsigned long long

    // Step 2: Prompt the user to enter a number
    printf("Enter a number to calculate its factorial: ");
    scanf("%d", &num);

    // Step 3: Ensure the number is non-negative
    if (num < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        // Step 4: Calculate the factorial using a loop
        for (int i = 1; i <= num; i++) {
            factorial *= i;
        }

        // Step 5: Display the result
        printf("Factorial of %d is %llu\n", num, factorial);
    }

    return 0;  // Step 6: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variable num is declared as an integer to store the number entered by the user. The variable factorial is declared as unsigned long long to store the factorial result since factorials can become very large.

Step 2: Input the Number

  • The program prompts the user to enter a number using printf. The scanf function then reads the input and stores it in the variable num.

Step 3: Ensure the Number is Non-Negative

  • The program checks if the input number is negative. If it is, a message is displayed stating that factorials are not defined for negative numbers.

Step 4: Calculate the Factorial

  • If the number is non-negative, a for loop is used to calculate the factorial:
    • The loop iterates from 1 to the input number num, multiplying the current value of factorial by the loop counter i.

Step 5: Display the Result

  • The program displays the calculated factorial using the printf function. The format specifier %llu is used for unsigned long long type.

Step 6: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter a number to calculate its factorial: 5
Factorial of 5 is 120

Example 2:

Enter a number to calculate its factorial: 7
Factorial of 7 is 5040

Example 3 (Negative Input):

Enter a number to calculate its factorial: -3
Factorial is not defined for negative numbers.

Conclusion

This C program demonstrates how to calculate the factorial of a number entered by the user. It covers basic concepts such as variable declaration, taking user input, handling edge cases (like negative numbers), and using loops for calculations. This example is useful for beginners learning C programming and understanding loops and conditional statements.

Leave a Comment

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

Scroll to Top