C Program to find Factorial of Number without using function

Find Factorial of Number without using function

Output :

Explanation of Program :

Before Explaining the program let us see how factorial is calculated –

Firstly accept the number whose factorial is to be found.

We have to iterate from 1 to (n-1) using for/while/do-while loop. In each iteration we are going to multiply the current iteration number and result.

Some Precautions to be taken :

Precaution 1 : Initialize ‘factorial’ variable

before going inside loop we must initialize factorial variable to 1 since by default each c variable have garbage value. If we forgot to initialize variable then garbage value will be multiplied and you will get garbage value as output.

Precaution 2 : For loop must start with 1

Suppose by mistake we write following statement –

then in the very first iteration we will get factorial = 0 and in all successive iteration we will get result as 0 since anything multiplied by Zero is Zero

Precaution 3 : Try to accept lower value to calculate factorial

We have 2 bytes to store the integer in Borland C++ compiler, so we can have maximum limit upto certain thousand. Whenever we try to accept value greater than 20 we will get factorial overflow.