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

  1. #include<stdio.h>  
  2.   
  3. void factorial(int);  
  4.   
  5. int main()  
  6. {  
  7.     int num;  
  8.   
  9.     printf("Enter a positive number to find Factorial\n");  
  10.     scanf("%d", &num);  
  11.   
  12.     factorial(num);  
  13.   
  14.     return 0;  
  15. }  
  16.   
  17. void factorial(int num)  
  18. {  
  19.     int count, fact = 1;  
  20.   
  21.     if(num == 0)  
  22.     {  
  23.         printf("Factorial of 0 is 1 (!0 = 1)\n");  
  24.     }  
  25.     else  
  26.     {  
  27.         for(count = 1; count <= num; count++)  
  28.         {  
  29.             fact = fact * count;  
  30.         }  
  31.   
  32.         printf("\nFactorial of %d is %d (!%d = %d)\n", num, fact, num, fact);  
  33.     }  
  34. }  

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 *