How To Write A C Program To Find Factorial Using Recursion

Guide on use recursion in c. Readers will also learn how to write a program to find the factorial of a number using recursive function. Necessary algorithms and code examples are given within the article.

How to write a C program to find the factorial of a number using recursion.

Recursion is a good technique in C which can be very beneficial in C if used in wise and error free manner. Recursion means the phenomenon of a function invoking itself. Use of recursion results in less amount of coding and lowers the bulk of the program but increases the logical complexity overall, so
practice recursion if you have a good hold on your algorithm and it will help you if used successfully. Finding a factorial of a number using recursion is a good way to understand functionality and implementation of recursive

Sequential Algorithm :

1.Take the number from user(say n,you will find the factorial of this number).
2.create an int variable (say var) and assign it to 1.
5.go to step 3 unless n==1
6.output var as the required factorial.

Recursive Algorithm :

module A(main function):
1.take the input from the user.
2.check and make sure it is a positive number.
3.pass the number to module B.
4.Print the value returned by module B as the required factorial.

module B(user defined function)
1.accept the value from calling module.
2.check if it is 1 , if yes return 1 to calling module.
3.decrement the value of the argument by 1.
4.pass it to module B.
5.get the value returned by module B and return it to calling module after multiplying it by the actual argument.



int fact(int n);

void main()
int n;
printf("/nEnter an integer:");
printf("/nThe factorial of % is = %d",n,fact(n));

int fact(int n)
return 1;

Explanation : Well it is actually pretty self explanatory, the main function is pretty basic and doing only input output work and calling the user defined function fact() with an argument. fact() receives an argument and checks if it is 1 , if yes it immediately sends back 1 to the calling function. In the else case it again calls the fact() function (itself) with the original argument decremented by one and returns to the calling function whatever value it is returned by the called function multiplied by the original argument. This calling the oneself thing with C functions is known as recursion.
Let us check how it works with an example, suppose the user enters 3 as input.

fact(3) is called

fact receives 3 , argument is not 1 , hence fact(2) is called

fact receives 2 , argument is not 1 , hence fact(1) is called

fact receives 1 , argument is 1 , hence 1 is returned

2*1 is returned

3*2*1 is returned

6 is printed as the factorial

understanding the work of functions in these different levels is all you need to understand how this program works. Hope I have done a good job explaining. But keep one thing in mind if not used correctly recursion can easily lead to never ending loops inside your program execution, for example in this case if we pass a negative number to fact() it will never become 1 no matter how many times we decrement it and the recursion process will go on endlessly. That's it now you know how to find the factorial of a number using recursion in C.

Related Articles

How to prevent acne scars?

This article explains various measures which keep acne under control. This decreases inflammation and consequently chances of formation of acne scars too decreases. Actions like not picking at skin, proper hydration, using sunscreen lotions, proper skin care regime etc prevent formation of acne scars.

How To Control Anger?

Have you ever been angry and later on felt sad for being angry on some one or something? Do you want to control your anger ? Is anger good or not ? The following article will discuss about anger and measures through which we can control our anger.

How to remove blood stains from delicate fabrics and specific surfaces?

This article explains various tips and methods to get rid of blood stains from delicate fabrics like silk, satin, woolen, linen etc and from specific surfaces like concrete, hardwood floors, mattress, quilt, leather, carpets etc. These methods used can make them free from blood stains.

Top 9 tips and exercises to sharpen the mind and boost brainpower

The brain needs constant stimulation to remain alert and active. Practice good habits that can sharpen your memory and help you stay alert and on top of your game at all times. Simple lifestyle changes that involve the number of you sleep or what you eat or how you relax can play a great role in how your brain functions. Find out what you need to do right, to get the best out of your grey cells.

How to become a pathologist with top hospitals in India

Are you looking for a career in medicine as a clinical pathologist? Need to know how to start a pathology lab? This article provides complete information on the different specializations in pathology and a career guide on starting a career as a pathologist. You will also get a general idea on the approximate costs of setting up your own path lab.

More articles: How to


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: