| | |
factorial using a for loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2004
Posts: 6
Reputation:
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.
C++ Syntax (Toggle Plain Text)
#include <iostream.h>
C++ Syntax (Toggle Plain Text)
#define q = 3
C++ Syntax (Toggle Plain Text)
int n; if (n < 0) return 0;
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
#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 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jul 2004
Posts: 6
Reputation:
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.C++ Syntax (Toggle Plain Text)
#include <iostream.h>I have no idea what this is intended to do.C++ Syntax (Toggle Plain Text)
#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.C++ Syntax (Toggle Plain Text)
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.C++ Syntax (Toggle Plain Text)
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.C++ Syntax (Toggle Plain Text)
#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 

C++ Syntax (Toggle Plain Text)
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
My Resume
My Resume
•
•
Join Date: Jul 2004
Posts: 1
Reputation:
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
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 
C++ Syntax (Toggle Plain Text)
{--------------------------------------------------------------------- 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
My Resume
My Resume
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- factorial numbers!!! (Java)
- Help with e^x problem please. (C++)
- c++ error factorial (C++)
Other Threads in the C++ Forum
- Previous Thread: Help with error C2228:
- Next Thread: Help with House Program
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






