Program To Find Factorial Of Number In C Language

0

Program To Find Factorial Of Number In C Language

This program calculates the factorial of a given number using recursion. The factorial() function is a recursive function that multiplies the number with the factorial of the number one less than itself until it reaches 1 or 0 (base case). The program prompts the user to input a number, and then it calculates and displays its factorial. If the user enters a negative number, the program informs that the factorial is not defined for negative numbers.

Program To Find Factorial Of Number In C Language


#include <stdio.h>

// Function to calculate the factorial of a number

unsigned long long factorial(int num) {

    if (num == 0 || num == 1) {

        return 1;

    } else {

        return num * factorial(num - 1);

    }

}

int main() {

    int number;

    printf("Enter a number to find its factorial: ");

    scanf("%d", &number);


    if (number < 0) {

        printf("Factorial is not defined for negative numbers.\n");

    } else {

        unsigned long long result = factorial(number);

        printf("Factorial of %d is %llu.\n", number, result);

    }


    return 0;


Output

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

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !