C Program To Find Factorial of a Number using Function


Write a function to calculate the factorial value of any integer entered through the keyboard.

Related Read:
C Program To Find Factorial of a Number

Factorial of a number is the product of all the numbers preceding it. For example, Factorial of 6 is 720 (1 x 2 x 3 x 4 x 5 x 6 = 720).

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Video Tutorial: C Program To Find Factorial of a Number using Function


[youtube https://www.youtube.com/watch?v=OcXCoX51Xx4]

YouTube Link: https://www.youtube.com/watch?v=OcXCoX51Xx4 [Watch the Video In Full Screen.]


Source Code: C Program To Find Factorial of a Number using Function

#include<stdio.h>

void factorial(int);

int main()
{
    int num;

    printf("Enter a positive number to find Factorial\n");
    scanf("%d", &num);

    factorial(num);

    return 0;
}

void factorial(int num)
{
    int count, fact = 1;

    if(num == 0)
    {
        printf("Factorial of 0 is 1 (!0 = 1)\n");
    }
    else
    {
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        printf("\nFactorial of %d is %d (!%d = %d)\n", num, fact, num, fact);
    }
}

Output 1:
Enter a positive number to find Factorial
5

Factorial of 5 is 120 (!5 = 120)

Output 2:
Enter a positive number to find Factorial
4

Factorial of 4 is 24 (!4 = 24)

Output 3:
Enter a positive number to find Factorial
6

Factorial of 6 is 720 (!6 = 720)

Output 4:
Enter a positive number to find Factorial
7

Factorial of 7 is 5040 (!7 = 5040)

Output 5:
Enter a positive number to find Factorial
8

Factorial of 8 is 40320 (!8 = 40320)

Logic To Find Factorial of a Number

Complete for loop logic to find Factorial of a number is present at C Program To Find Factorial of a Number. Watch the video without fail to understand the logic.

Note: Function factorial doesn’t return anything so its return type is void. It accepts 1 integer type argument.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

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