473,385 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

calculating n factorial

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;
}
Oct 19 '08 #1
2 4872
archonmagnus
113 100+
For one thing, it looks like you have an excess in the number of braces you have. Specifically, in the segment:
Expand|Select|Wrap|Line Numbers
  1.     else if(n<=50)
  2.     {
  3.         {for(int i=n; i>=1; i--)
  4.         {
  5.             factorial *= i;
  6.         }
  7.  
  8.         cout << "n! == " << factorial << endl;
  9.     }
  10.     else
  11.     {
  12.         {lnFactorial = (n*log(n)) - n;
  13.         factorial = exp(lnFactorial);
  14.         }
  15.         cout <<"n! == " << factorial << endl;
  16.     }
  17.  
You have extra braces preceeding "for(int i=n; i>=1; i--)" and "InFactorial = (n*log(n)) - n;". Please don't think I'm criticizing your code, but if I may suggest using a better visualization/spacing technique, it would help you to be able to see these fixes a bit easier.
Oct 20 '08 #2
whodgson
542 512MB
I suggest you write a simple factorial fuction and then a basic test driver. You can write a fancy main later. Also it would help if you enclosed your
Expand|Select|Wrap|Line Numbers
  1. code
say:
Expand|Select|Wrap|Line Numbers
  1. double fact (int n)
  2. {
  3.     if (n<0) return 0;
  4.       double f=1;
  5.        while(n>1)
  6.           f*=n--;
  7.           return f;
  8. }
you can substitute your function for the above later after it compiles and runs properly The test driver only needs a for loop from within which you call fact()
Oct 20 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
35
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
8
by: salman | last post by:
this program is giving compile time error. so plse ge me the logic of factorial # include <iostream.h> # include <math.h> void main() { int f,sum=0,i,j,n; cout<<"\nEnter Number: ";
0
by: outofstep | last post by:
Hit me up on AIM/YIM (oipaloi). I'm not a code monkey and would like to get some advice. Or hit up this thread right here. I'm using DEV C++ as my compiler. I'm trying to calculate...
10
by: Lisa | last post by:
In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation...
59
by: Umesh | last post by:
i wrote the following program to calculate factorial: #include<stdio.h> #include<iostream.h> void main() { int i,n; long int p=1; // or long double p=1; for exponential result which I don't...
3
by: Sugandh Jain | last post by:
Hi. How to write a function that will return me the factorial (say in a string) for the any positive integer it takes? When we find a factorial of even say 2000 or a higher number, it will be...
0
kadghar
by: kadghar | last post by:
Hi, I saw that Killer posted a simple Factorial Function that allows you to calculate up to 13!, well, you can use this for bigger numbers by changing the variable type. Why is this? You can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.