# C++/recurser

Question
Good day,,my problem is:
use recursion to write a program that returns the factorial of any number greater than 0. (Factorial is number*number-1*number-2...*1)
Hope you can help!thankS:]

Hi, Isheen.

The factorial of an integer (non-negative) is defined as the product of every positive integer less than or equal to the integer.  The symbol for a factorial is the exclamation point (!).  So, the factorial of A (represented as A!) would be the product of every integer from 1 to A.  5!, for example, is:

1 * 2 * 3 * 4 * 5 = 120

The algorithm for this is really quite simple and makes for an easy recursive function:

int factorial(int num)
{
if (num == 1)
return num;
else
return factorial(num - 1) * num;
}

You can see here how the function calls itself, decrementing num by one.  In this manner, there is a change in the input value and a defined end point (num == 1).  Once the end point is reached, the return values cascade back until the initial caller of the recursive function is reached.

If you have any further questions, or if I have raised any new questions for you, please do not hesitate to ask.  I'm here to help. :)

C++

Volunteer

#### Joseph Moore

##### Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

##### Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004