I need to write a program which will execute a factorial, but with an upper and lower bound. Here's what I've written, but it's not working. I keep getting an error message on the for command with expression syntax. Please, I've got to have this and much much more by wednesday.

Thanks!

``````#include <iostream.h>

#define q = 3

int main()

{
int n;
if  (n < 0) return 0;
int f;

for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}

cout<< f;
char w;
cin>>w;
return 0;
}``````

``#include <iostream.h>``

You shouldn't have the equals sign here.

``#define q = 3``

I have no idea what this is intended to do.

``````int n;
if (n < 0) return 0;``````

see this example dude ;)

``````int main()
{
int num;
do{
cout << "Enter a number between 1 and 10";
cin>>num;
}while( num < 1 || num > 10 ); /* ask some more if we dont get something between 1 and 10 */
double factorial( num …``````

You cant use `int` for such big numbers as 100!, since `int` usually can handle 32bit value.
solution is google: "c++ big integer"

1.) I suggest you take a closer look at your datatype limits. A 32-bit signed integer can hold values ranging from about -2.1 billion to +2.1 billion. …

Please ignore fr3aky's poorly formatted unreadable code. It will teach you very little that is useful.

fr3aky, here we do not not give programs to people, we help them solve their own problems. And when we post code the help, the code is formatted, readable, and useful.

here we do not not give programs to people,

Ah, so you do give programs to people?

//just kidding...double negative there, lol

## All 17 Replies

``#include <iostream.h>``

You shouldn't have the equals sign here.

``#define q = 3``

I have no idea what this is intended to do.

``````int n;
if (n < 0) return 0;``````

Again, your code it a little unclear here. It says to declare a variable f, set it equal to 1 and then multiply by n. This f and the int f previously declared are different.

``````for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}``````

When the loop finishes, the double f should go out of scope. So the value that is being printed at the end is the uninitialized value of the int f.

(If it were to compile.)

It couldn't hurt to use more descriptive names, too. Maybe something like this.

``````#include <iostream> // let's try to use a compiler from this millenium

int main()
{
int n, result = 1, value = 3;
for ( n = 1; n <= value; n++ )
{
result *= n;
}
std::cout << value << "! = " << result << std::endl;
std::cin.get(); // pause ???
return 0;
}

/* my output
3! = 6
*/``````

Thanks, I'll try and integrate this into my monster program.

``#include <iostream.h>``

You shouldn't have the equals sign here.

``#define q = 3``

I have no idea what this is intended to do.

``````int n;
if (n < 0) return 0;``````

Again, your code it a little unclear here. It says to declare a variable f, set it equal to 1 and then multiply by n. This f and the int f previously declared are different.

``````for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}``````

When the loop finishes, the double f should go out of scope. So the value that is being printed at the end is the uninitialized value of the int f.

(If it were to compile.)

It couldn't hurt to use more descriptive names, too. Maybe something like this.

``````#include <iostream> // let's try to use a compiler from this millenium

int main()
{
int n, result = 1, value = 3;
for ( n = 1; n <= value; n++ )
{
result *= n;
}
std::cout << value << "! = " << result << std::endl;
std::cin.get(); // pause ???
return 0;
}

/* my output
3! = 6
*/``````

see this example dude ;)

``````int main()
{
int num;
do{
cout << "Enter a number between 1 and 10";
cin>>num;
}while( num < 1 || num > 10 ); /* ask some more if we dont get something between 1 and 10 */
double factorial( num );
for( int n(num-1) ; n > 0 ; factorial *= n, --n); /* demonstration of how the for loop can be extremely useful */
cout << "The factorial of "<< num << " is "<< factorial << endl;
}``````

Hi Guys
actually it is the first time to write to you
i have only 3 quistions and if any one of you has the answer for it please give it to me cause i need it very very urgent:

the first quistion is for factorial also but in PASCAL :
i want to have a program written in PASCAL that calculate the factorial ..but this program must contain stacks..i gave up looking fo the answer cause i didnt found it :( so pleaaase heeeelp meee.

the second quistion is in PASCAL also :
i want to write a program in PASCAL using arrays that can recieve marks of 10 students in 5 subjects then calculate :
1- the average of every student
2 - the average of every subject
3 - the general average of all subjects an all students
4 - the highest mark & the lowest mark.

the third one is in C++ .. i need a programe for three hotdog shops
that recieve how many hotdog and how many bread in each shop then recordin every sale process by losing 1 bread & 1 hot dog every time
and i can ask at any place in programm about the amount of hotdog and bread in each shop
also it must contain a summary at the end of the day about how many bread & hot dog at the end of the day.

well .. i know it is too defficult but please i want it urgent as soon as posiple
please send it to my email at
<<snip>>

i can help u with the first .. cuz really i'm not expert with pascal but i know little about it . :) .. see this code this what i learned in pascal :D

``````{---------------------------------------------------------------------
PROGRAM: FACTORIAL2.PAS
Version:				   Turbo Pascal 6.0
-------------------------------------------------------------------}
PROGRAM Factrl2 (Input, Output);
USES
Crt;
CONST
MaxM  =  7;
VAR
Choice,
M			:  Integer;
UserQuits	:  BOOLEAN;
{------------------------------------}
{			PROCEDURES			  }
{------------------------------------}
{-----------------------------}
{		  INIT.PROC		  }
{-----------------------------}
PROCEDURE Init;
BEGIN
TextBackground(Blue);				   { Sets colors					 }
TextColor(White);
Window(0,0,80,25);
ClrScr;
END;

{-----------------------------}
{		 .PROC		   }
{-----------------------------}
PROCEDURE Null;
BEGIN
END;
{-----------------------------}
{	  FACTFORDO .PROC		}
{-----------------------------}
PROCEDURE FactForDo;
VAR
k,								   { index }
FAC,
Factorial		 :  INTEGER;		{ partial factorial }
BEGIN
ClrScr;
WriteLn;
Write(' ':20,' Enter an integer - 0 to ',MaxM,': ');
IF M <= MaxM THEN
BEGIN
FAC := 1;
FOR k := 2 to M DO
FAC := FAC * K;
Factorial := FAC;
WriteLn;
WriteLn(' ':20,'		  M was entered as: ', M );
WriteLn;
WriteLn(' ':20,'	   M!, or M Factorial = ', Factorial );
END
ELSE
BEGIN
WriteLn;
WriteLn('The number you entered is greater than ',MaxM,'.  Re-enter. ');
END;  { ELSE }
WriteLn;
END;	  {FactForDo}

{-----------------------------}
{	  FACTORIAL.PROC		 }
{-----------------------------}
FUNCTION Factorial ( M : Integer ) : Integer;
BEGIN
IF M = 0 THEN
Factorial := 1
ELSE
Factorial := M * Factorial( M-1 );
END;	  { Factorial }

{-----------------------------}
{		FACTREC.PROC		 }
{-----------------------------}
PROCEDURE FactRec;
BEGIN
ClrScr;
WriteLn;
Write(' ':20,'Enter an integer - 0 to ',MaxM,': ');
WriteLn;
IF M > MaxM THEN
WriteLn('The number you entered is greater than ',MaxM,'.  Re-enter. ')
ELSE
BEGIN
WriteLn;
WriteLn(' ':20,'		  M was entered as: ', M );
M := Factorial( M );
END;  { ELSE }
WriteLn;
WriteLn(' ':20,'	   M!, or M Factorial = ', M );
WriteLn;
END;	  {FactRec}

{------------------------------------}
{		   MAIN PROGRAM			 }
{------------------------------------}
BEGIN
Init;
UserQuits := False;
REPEAT
ClrScr;
WriteLn;
WriteLn(' ':25, ' Menu For Factorial Methods ' );
WriteLn;
WriteLn(' ':20, ' 1.  Factorial Using a FOR DO Loop' );
WriteLn(' ':20, ' 2.  Factorial Using Recursion'	 );
WriteLn(' ':20, ' 3.  Quit '						 );
CASE Choice OF
1  :  FactForDo;
2  :  FactRec;
3  :  UserQuits := True;
END;
UNTIL UserQuits;
END.``````

Hi Guys>>
It is first time to write 4 u but actually I have one eq only >> so if u can help>>it is also about the factorial by using loop>>
the Q is

The value of Euler's number e,can be approximates using this formula
e=1+1/1!+1/2!+1/3!+1/4!........
using this formula,write a c++ program that approximates the value of e using a while loop that terminates when the difference between two successive approximates differ by less than 10^-9.

Thanx any way

commented: thread necro cheater, not cool +0
commented: We don't do other people's homework. Show what you've done and we'll help, but don't expect someone to do it for you. +0

``#include <iostream.h>``

You shouldn't have the equals sign here.

``#define q = 3``

I have no idea what this is intended to do.

``````int n;
if (n < 0) return 0;``````

Again, your code it a little unclear here. It says to declare a variable f, set it equal to 1 and then multiply by n. This f and the int f previously declared are different.

``````for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}``````

When the loop finishes, the double f should go out of scope. So the value that is being printed at the end is the uninitialized value of the int f.

(If it were to compile.)

It couldn't hurt to use more descriptive names, too. Maybe something like this.

``````#include <iostream> // let's try to use a compiler from this millenium

int main()
{
int n, result = 1, value = 3;
for ( n = 1; n <= value; n++ )
{
result *= n;
}
std::cout << value << "! = " << result << std::endl;
std::cin.get(); // pause ???
return 0;
}

/* my output
3! = 6
*/``````

i had it and founds that it just worked for small integer. when i try to generate 100! it turns out zero. how to solve the problem?

commented: Hmm. I've lost my train of thought since 2004. How about starting a new thread? -2

You cant use `int` for such big numbers as 100!, since `int` usually can handle 32bit value.
solution is google: "c++ big integer"

I need to write a program which will execute a factorial, but with an upper and lower bound. Here's what I've written, but it's not working. I keep getting an error message on the for command with expression syntax. Please, I've got to have this and much much more by wednesday.

Thanks!

#include <iostream.h>

#define q = 3

int main()

{
int n;
if (n < 0) return 0;
int f;

for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}

cout<< f;
char w;
cin>>w;
return 0;
}

----------------------------------------------------------------------------------------
Try using array for your data set. For instance:
i

``````nt numbers;//setup array
numbers=1; //factorial of 0 is 1
int factorial = 0;//initialize your factorial

//loop will multiply all the numbers in the array
for (int i = 1; i < 50; i ++){
factorial = numbers[i] * numbers[i -1];

}

cout <<"\nFactorial is: "<< factorial << endl;``````

##############---------
Don't forget to load array with the data set - where you can use another for loop to make achieve that, you can take as input lower and upper bound and set them in array as numbers[lower], numbers[upper], and you should have your program - keep in mind arrays starts as array and finishes as array;

Cheers.

You cant use `int` for such big numbers as 100!, since `int` usually can handle 32bit value.
solution is google: "c++ big integer"

1.) I suggest you take a closer look at your datatype limits. A 32-bit signed integer can hold values ranging from about -2.1 billion to +2.1 billion. A long long can go out to about +/- 9 Quintillion (yes Quintillion is a real number, it's 18-zeros)

2.) Why are we posting in a 5-year-old thread??? Especially when the thread necro is just someone trying to scam a homework assignment...

well to keep kicking the dead dog. Fboady it is true that a 32bit int is aprox -2 bil to 2 bil but 100! is 9.3326215443944152681699238856267e+157 which is way out of range for an int on even a 128 bit system.

well to keep kicking the dead dog. Fboady it is true that a 32bit int is aprox -2 bil to 2 bil but 100! is 9.3326215443944152681699238856267e+157 which is way out of range for an int on even a 128 bit system.

You are correct.

I realized what pecet was actually saying way after I posted. I completely overlooked the factorial symbol as merely an exclamation point.

I was wrong :\$

``````#include<iostream>
using namespace std;
int main()
{
long int n,i;
long int result=1;
cin>>n;
if(n==0 || n==1)
cout<<1<<endl;
else if(n<0)
return 0;
else
for(i=1;i<=n;i++)
result *=i;
cout<<result<<endl;
return 0;
}``````

Please ignore fr3aky's poorly formatted unreadable code. It will teach you very little that is useful.

fr3aky, here we do not not give programs to people, we help them solve their own problems. And when we post code the help, the code is formatted, readable, and useful.

here we do not not give programs to people,

Ah, so you do give programs to people?

//just kidding...double negative there, lol

commented: oops.... that was not not right, was it it? +11
``````#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int numbers;//setup array
numbers=1; //factorial of 0 is 1
int factorial = 0;//initialize your factorial

//loop will multiply all the numbers in the array
for (int i = 1; i < 50; i ++)
{
factorial = numbers[i] * numbers[i -1];
}

cout <<"\nFactorial is: "<< factorial << endl;

system ("PAUSE");
return 0;
}``````

The output is 687194768.
It doesn't make sense!