•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,850 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,345 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 7101 | Replies: 5
![]() |
•
•
Join Date: Jul 2004
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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;
}
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;
}
The following header is deprected, instead try to use <iostream> You shouldn't have the equals sign here. I have no idea what this is intended to do. 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. 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.h>
#define q = 3
int n; if (n < 0) return 0;
for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}(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
*/•
•
Join Date: Jul 2004
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Thanks, I'll try and integrate this into my monster program.
•
•
•
•
Originally Posted by Dave Sinkula
The following header is deprected, instead try to use <iostream>You shouldn't have the equals sign here.#include <iostream.h>I have no idea what this is intended to do.#define q = 3Again, 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.int n; if (n < 0) return 0;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.for (n=1; n <= q; n++) { double f = 1; {f *= n;} }
(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 */
•
•
Join Date: May 2004
Location: Egypt - Cairo
Posts: 129
Reputation:
Rep Power: 5
Solved Threads: 2
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;
} Real Eyes Realize Real Lies
•
•
Join Date: Jul 2004
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
sa7ari_alshooq@yahoo.com
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
sa7ari_alshooq@yahoo.com
•
•
Join Date: May 2004
Location: Egypt - Cairo
Posts: 129
Reputation:
Rep Power: 5
Solved Threads: 2
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 
.. see this code this what i learned in pascal 
{---------------------------------------------------------------------
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,': ');
ReadLn(M);
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;
WriteLn(' Press <ENTER> to return to menu.');
ReadLn;
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,': ');
ReadLn(M);
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;
WriteLn(' Press <ENTER> to return to menu.');
ReadLn;
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 ' );
ReadLn ( Choice );
CASE Choice OF
1 : FactForDo;
2 : FactRec;
3 : UserQuits := True;
END;
UNTIL UserQuits;
END. Real Eyes Realize Real Lies
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Factorial of a number (C)
- having problem while creating factorial (C#)
- Help ME (C++)
Other Threads in the C++ Forum
- Previous Thread: Double Linked Lists and Functions required
- Next Thread: C++ Builder 6



Linear Mode