Hi,
I'm trying to write a program to calculate n factorial but it won't compile. Can anyone tell me what I'm doing wrong?
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n;
float factorial;
float lnFactorial;
cout << "Please enter a positive integer:" << endl;
cin >> n;
{
if (n%2 == 0){
}
else if ((n+1)%2==0){
}
else {
cout << "Please enter a positive integer:" << endl;
}
}
if (n<0){
cout << "Please enter a positive integer:" << endl;
cin >> n;
}
else if(n==0){
cout <<"n! == 1" << endl;
}
else if(n<=50) {
{for(int i=n; i>=1; i--){
factorial *= i;
}
cout << "n! == " << factorial << endl;
}
else{
{lnFactorial = (n*log(n)) - n;
factorial = exp(lnFactorial );
}
cout <<"n! == " << factorial << endl;
}
return 0;
}
I'm trying to write a program to calculate n factorial but it won't compile. Can anyone tell me what I'm doing wrong?
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n;
float factorial;
float lnFactorial;
cout << "Please enter a positive integer:" << endl;
cin >> n;
{
if (n%2 == 0){
}
else if ((n+1)%2==0){
}
else {
cout << "Please enter a positive integer:" << endl;
}
}
if (n<0){
cout << "Please enter a positive integer:" << endl;
cin >> n;
}
else if(n==0){
cout <<"n! == 1" << endl;
}
else if(n<=50) {
{for(int i=n; i>=1; i--){
factorial *= i;
}
cout << "n! == " << factorial << endl;
}
else{
{lnFactorial = (n*log(n)) - n;
factorial = exp(lnFactorial );
}
cout <<"n! == " << factorial << endl;
}
return 0;
}
Comment