Find the Factorial of a Number: C

Advertisement:

This video tutorial illustrates finding factorial of a given number using C programming.

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Ex:
if user enters 5, then the factorial is 1 * 2 * 3 * 4 * 5 i.e., factorial = 120
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
19
#include < stdio.h >
#include < conio.h >
 
void main()
{
	int num, i, fact = 1;
	clrscr();
 
	printf("Enter a number\n");
	scanf("%d", &num);
 
	for( i = 1; i < = num; i++ )
	 fact = fact * i;                // fact *= i;
 
	printf("\nFactorial of %d is %d", num, fact);
 
	getch();
 
}

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

Note: Factorial of 0 is 1. i.e., 0! = 1

factorial-cpp


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

Video Tutorial: Factorial in c


You need to a flashplayer enabled browser to view this YouTube video

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



Output:
Enter a number
5
Factorial of 5 is 120

Also Watch/Read: Find the Factorial of a Number: C++

Leave a Reply

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