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
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the number and the factorial result.
- Input the Number: Use
scanfto take input from the user for the number. - Calculate the Factorial: Use a loop to multiply the integers from 1 to the input number.
- Display the Result: Use
printfto 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
numis declared as an integer to store the number entered by the user. The variablefactorialis declared asunsigned long longto 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. Thescanffunction then reads the input and stores it in the variablenum.
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
forloop is used to calculate the factorial:- The loop iterates from 1 to the input number
num, multiplying the current value offactorialby the loop counteri.
- The loop iterates from 1 to the input number
Step 5: Display the Result
- The program displays the calculated factorial using the
printffunction. The format specifier%lluis used forunsigned long longtype.
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.