stevenviney
- 3
- 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??
#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??