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

Find the Factorial of a Number: C++

Video tutorial to find the factorial of a number: if the user enters 3, then the factorial is 1 * 2 * 3 i.e., factorial = 6
This logic must be handled with a c++ program.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream .h>
#include<conio .h>
 
void main()
{
  int fact = 1, N;
  clrscr();
 
  cout< <endl<<"Enter a number\n";
  cin>>N;
 
  for( int i=1; i< =N; i++ )
   fact = fact * i;  // OR fact *= i;
 
  cout<<endl<<endl<<"Factorial of "<<N<<" is "<<fact;
 
  getch();
}

Here the for loop stars from 1 and not 0. As anything multiplied by 0 would also be zero.

factorial-cpp


fact *= i; is the compact representation of fact = fact * i;

Video Tutorial: Factorial in cpp


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

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



Output:
Enter a number
5
Factorial of 5 is 120