Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

# c++

3 replies to this topic

### #1 nickautomatic

nickautomatic

• Members
• 143 posts
• OFFLINE
•
• Gender:Male
• Local time:05:09 PM

Posted 02 October 2015 - 08:30 PM

Can anyone give me a sample C program for factorial? A program will ask a user to enter an integer.

sample result

Enter an integer:

the factorial of 5 is 120.

### #2 Slurppa

Slurppa

• Malware Study Hall Senior
• 666 posts
• OFFLINE
•
• Gender:Male
• Local time:10:09 AM

Posted 03 October 2015 - 11:12 AM

### #3 nickautomatic

nickautomatic
• Topic Starter

• Members
• 143 posts
• OFFLINE
•
• Gender:Male
• Local time:05:09 PM

Posted 06 October 2015 - 04:59 PM

thank you, Slurppa.

Edited by nickautomatic, 06 October 2015 - 05:00 PM.

### #4 AceInfinity

AceInfinity

• Members
• 30 posts
• OFFLINE
•
• Gender:Male
• Local time:01:09 AM

Posted 09 January 2016 - 10:33 PM

You can use a recursive function for calculating factorials. I seen a video posted by another member on a different forum in which was from a Harvard student attempting to explain recursion, and factorial was the prime example but she didn't consider that 0! == 1, and the recursive approach she was demonstrating was one that led to a stackoverflow in such a case. Just a point to show that recursion isn't always the easiest thing even for supposedly some of the brightest student minds.

Here's a recursive factorial function:

```unsigned long factorial(unsigned int n)
{
return n ? n * factorial(n - 1) : 1;
}

```

Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users