Calculating Factorials: A Program to Find Factorial of a Number in c

29 Dec 2022 Balmiki Kumar 0 C Programming

Coding Factorial Calculation: Program to Compute Factorial of a Given Number

A C program to find the factorial of a given number is a program that calculates the product of all positive integers up to a given number. The factorial of a non-negative integer 'n' is denoted by 'n!' and is defined as the product of all positive integers from 1 to n. For example, the factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.

 C Program to calculate the factorial value using recursion.

C programming

#include<stdio.h>
int fact(int n);
int main() {
 int x, i;
 printf("Enter a value for x: \n");
 scanf("%d", &x);
 i = fact(x);
 printf("\nFactorial of %d is %d", x, i);
 return 0;
}
int fact(int n) {
 /* n=0 indicates a terminating condition */
 if (n <= 0) {
 return (1);
 } else {
 /* function calling itself */
 return (n * fact(n - 1));
 /*n*fact(n-1) is a recursive expression */
 }
}
Output:
Enter a value for x:
4
Factorial of 4 is 24

Explanation:

fact(n) = n * fact(n-1)
 If n=4
 fact(4) = 4 * fact(3) there is a call to fact(3)
 fact(3) = 3 * fact(2)
 fact(2) = 2 * fact(1)
 fact(1) = 1 * fact(0)
 fact(0) = 1
 fact(1) = 1 * 1 = 1
 fact(2) = 2 * 1 = 2
 fact(3) = 3 * 2 = 6
 Thus fact(4) = 4 * 6 = 24
Terminating condition(n <= 0 here;) is a must for a recursive program. Otherwise the program enters into an
infinite loop

C -Program to find the factorial of a given number using a for loop

#include<stdio.h>
int main()
{
    int n, i;
    unsigned long long factorial = 1;
    
    printf("Enter an integer: ");
    scanf("%d", &n);
    
    // Check if the input number is negative
    if (n < 0)
        printf("Error: Factorial of a negative number doesn't exist.");
    else
    {
        for (i = 1; i <= n; ++i)
        {
            factorial *= i;   // factorial = factorial * i;
        }
        
        printf("Factorial of %d = %llu", n, factorial);
    }
    
    return 0;
}

Attention : The above program prompts the user to enter an integer and calculates the factorial of the given number using a for loop. It also checks if the input number is negative, in which case it displays an error message. The result is then displayed on the screen.

Further Reading:

Calculating Factorials: A Program to Find Factorial of a Number

Design a function to finding factorial of given number in c

Design a recursive Function For Finding The Factorial of given Number in c

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Kumar

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.