C/C++ How to Calculate Factorial and Apply Stirling's Approximation in C++?

AI Thread Summary
The discussion centers on creating a program that calculates the factorial of a positive integer N, with additional requirements for handling integers greater than 50 using Stirling's approximation. The initial code provided successfully calculates the factorial but lacks input validation for positive integers and the implementation of Stirling's approximation. To address these issues, the program needs to include a loop that prompts the user to re-enter the integer if it is not positive. Additionally, an if statement should be added to apply Stirling's approximation when N exceeds 50, which requires including a math library for the natural logarithm function. The key points emphasize the need for input validation and the correct application of mathematical approximations in programming.
stevenviney
Messages
3
Reaction score
0
I need to write a program that prompts me to enter a positive integer N, after doing so it calculates the factorial of that integer and prints it. I am able to do this with the following code.

#include <iostream>
using namespace std;
int main()
{
double I, N, Factorial = 1; // Do not forget to initialize Factorial
cout << "Enter a positive integer:" << endl;
cin >> N;
I = 1; // This initialization step has to be done. And it must be done before the while loop.
while (I <= N) // Notice there is no semicolon here.
{
Factorial = Factorial * I; // The body of the loop begins here.
I = I + 1; // You must increment I. The body of the loop ends here.
}
cout << Factorial << endl;
return 0;
}


I now however need to create the condition that IF the integer is greater than 50, I need to apply Stirling's approximation which is = N (NaturalLog(N)) - N. I also need the program to initially check whether or not the integer is positive, and if not to prompt to re-enter.

Can anyone help??
 
Technology news on Phys.org
The first is just a simple if statement. You'll need to include a math library to get the log. For the second, you should loop until the integer is positive. That is, you input the integer the first time, and then enter a loop so that while the integer is not positive, you tell them so and ask them for it again.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top