C Program to Find Factorial of a Number


Hello Readers 🙂 Hope you are getting valuable posts. If you have any query or want to know about any program, Let Me Know, I’ll love to help you out. And Keep Learning.

Now I am going to write a program to find the factorial of a number. Factorial of a number is the multiplication of all numbers lower than it and greater than zero as factorial of 5 will be 5*4*3*2*1=120.

Let’s Make The Program ►►

#include<stdio.h>
#include<conio.h>
 
void main()
{
   int num, fact=1, i;
   printf("Enter Number to find it's factorial");
   scanf("%d",&num);
 
   for ( i=num ; i >= 1 ; i--,num-- )
      fact=fact*num;

printf("Factorial of the number is %d\n",fact);
 
getch();   
}

Explanation : For example we want to find the factorial of 5 we know that the factorial of 5 will be 5*4*3*2*1=120. So in the for loop we initialize i=num; to start from 5 and decrement it every after the statement after for loop executed by 1 using i–; also we decrement the variable num as for when i=5, num is also equals to 5 then after multiply with fact, we decrement it, now the num will be 4 and so on.

Remmeber while declaring the variable fact, initialize it with 1 otherwise any garbage value will be multiply with the number.

Advertisements

2 thoughts on “C Program to Find Factorial of a Number”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s