C program to find factorial of number - Explanation - Download
Hi
everyone,
In
this post, we are going to discuss, factorial of a number in the
simplest way.
We
are also adding a c program and its explanation.
Now
,what is a factorial.
Factorial
of a number,is the product of all natural numbers upto that number.
That
is, factorial of a number say 5 is given by,
Factorial=1*2*3*4*5
Factorial
of 0 is defined as one.
Have
a look at the flowchart of the program.Based on this we are program.
After
starting program,we will ask the user to input number,say n.
Then
we will check whether the number is a whole number.
Since factorial is
not defined for negative numbers.Now the logic of the program
enters.
We are considering the series of multiplications in a loop.
I
is the loop variable.We are incrementing the value of I after every
multiplication.
The working is like that.First we will check if I is
less than n.
If yes we will multiple it with fact.this process
continues till the condition fails.
Now we have factorial in
fact,display it.This is the basic logic of program.
If
you find any difficulty,check this example.
Let
n=3.
First
i=1.
Check
i<=3,
Fact=1*1.
I
is incremented.i=2 now.
Fact
changes to 1*2.
I
is incremented and equals to 3.
Fact
changes to 2*3=6.
Program
stops.
C Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact;
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial of %d = %d",n,fact);
getch();
}