factorial using a for loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 6
Reputation: ellisrn is an unknown quantity at this point 
Solved Threads: 0
ellisrn ellisrn is offline Offline
Newbie Poster

factorial using a for loop

 
1
  #1
Jul 19th, 2004
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;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: factorial using a for loop

 
0
  #2
Jul 19th, 2004
The following header is deprected, instead try to use <iostream>
  1. #include <iostream.h>
You shouldn't have the equals sign here.
  1. #define q = 3
I have no idea what this is intended to do.
  1. int n;
  2. 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.
  1. for (n=1; n <= q; n++)
  2. {
  3. double f = 1;
  4. {f *= n;}
  5. }
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.
  1. #include <iostream> // let's try to use a compiler from this millenium
  2.  
  3. int main()
  4. {
  5. int n, result = 1, value = 3;
  6. for ( n = 1; n <= value; n++ )
  7. {
  8. result *= n;
  9. }
  10. std::cout << value << "! = " << result << std::endl;
  11. std::cin.get(); // pause ???
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. 3! = 6
  17. */
"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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 6
Reputation: ellisrn is an unknown quantity at this point 
Solved Threads: 0
ellisrn ellisrn is offline Offline
Newbie Poster

Re: factorial using a for loop

 
0
  #3
Jul 19th, 2004
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>
  1. #include <iostream.h>
You shouldn't have the equals sign here.
  1. #define q = 3
I have no idea what this is intended to do.
  1. int n;
  2. 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.
  1. for (n=1; n <= q; n++)
  2. {
  3. double f = 1;
  4. {f *= n;}
  5. }
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.
  1. #include <iostream> // let's try to use a compiler from this millenium
  2.  
  3. int main()
  4. {
  5. int n, result = 1, value = 3;
  6. for ( n = 1; n <= value; n++ )
  7. {
  8. result *= n;
  9. }
  10. std::cout << value << "! = " << result << std::endl;
  11. std::cin.get(); // pause ???
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. 3! = 6
  17. */
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 141
Reputation: meabed is on a distinguished road 
Solved Threads: 3
Team Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: factorial using a for loop

 
0
  #4
Jul 20th, 2004
see this example dude
  1. int main()
  2. {
  3. int num;
  4. do{
  5. cout << "Enter a number between 1 and 10";
  6. cin>>num;
  7. }while( num < 1 || num > 10 ); /* ask some more if we dont get something between 1 and 10 */
  8. double factorial( num );
  9. for( int n(num-1) ; n > 0 ; factorial *= n, --n); /* demonstration of how the for loop can be extremely useful */
  10. cout << "The factorial of "<< num << " is "<< factorial << endl;
  11. }
Real Eyes Realize Real Lies
My Resume
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1
Reputation: ahsn is an unknown quantity at this point 
Solved Threads: 0
ahsn ahsn is offline Offline
Newbie Poster

Re: factorial using a for loop

 
0
  #5
Jul 20th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 141
Reputation: meabed is on a distinguished road 
Solved Threads: 3
Team Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: factorial using a for loop

 
0
  #6
Jul 20th, 2004
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
  1. {---------------------------------------------------------------------
  2. PROGRAM: FACTORIAL2.PAS
  3. Version: Turbo Pascal 6.0
  4. -------------------------------------------------------------------}
  5. PROGRAM Factrl2 (Input, Output);
  6. USES
  7. Crt;
  8. CONST
  9. MaxM = 7;
  10. VAR
  11. Choice,
  12. M : Integer;
  13. UserQuits : BOOLEAN;
  14. {------------------------------------}
  15. { PROCEDURES }
  16. {------------------------------------}
  17. {-----------------------------}
  18. { INIT.PROC }
  19. {-----------------------------}
  20. PROCEDURE Init;
  21. BEGIN
  22. TextBackground(Blue); { Sets colors }
  23. TextColor(White);
  24. Window(0,0,80,25);
  25. ClrScr;
  26. END;
  27.  
  28. {-----------------------------}
  29. { .PROC }
  30. {-----------------------------}
  31. PROCEDURE Null;
  32. BEGIN
  33. END;
  34. {-----------------------------}
  35. { FACTFORDO .PROC }
  36. {-----------------------------}
  37. PROCEDURE FactForDo;
  38. VAR
  39. k, { index }
  40. FAC,
  41. Factorial : INTEGER; { partial factorial }
  42. BEGIN
  43. ClrScr;
  44. WriteLn;
  45. Write(' ':20,' Enter an integer - 0 to ',MaxM,': ');
  46. ReadLn(M);
  47. IF M <= MaxM THEN
  48. BEGIN
  49. FAC := 1;
  50. FOR k := 2 to M DO
  51. FAC := FAC * K;
  52. Factorial := FAC;
  53. WriteLn;
  54. WriteLn(' ':20,' M was entered as: ', M );
  55. WriteLn;
  56. WriteLn(' ':20,' M!, or M Factorial = ', Factorial );
  57. END
  58. ELSE
  59. BEGIN
  60. WriteLn;
  61. WriteLn('The number you entered is greater than ',MaxM,'. Re-enter. ');
  62. END; { ELSE }
  63. WriteLn;
  64. WriteLn(' Press <ENTER> to return to menu.');
  65. ReadLn;
  66. END; {FactForDo}
  67.  
  68. {-----------------------------}
  69. { FACTORIAL.PROC }
  70. {-----------------------------}
  71. FUNCTION Factorial ( M : Integer ) : Integer;
  72. BEGIN
  73. IF M = 0 THEN
  74. Factorial := 1
  75. ELSE
  76. Factorial := M * Factorial( M-1 );
  77. END; { Factorial }
  78.  
  79. {-----------------------------}
  80. { FACTREC.PROC }
  81. {-----------------------------}
  82. PROCEDURE FactRec;
  83. BEGIN
  84. ClrScr;
  85. WriteLn;
  86. Write(' ':20,'Enter an integer - 0 to ',MaxM,': ');
  87. ReadLn(M);
  88. WriteLn;
  89. IF M > MaxM THEN
  90. WriteLn('The number you entered is greater than ',MaxM,'. Re-enter. ')
  91. ELSE
  92. BEGIN
  93. WriteLn;
  94. WriteLn(' ':20,' M was entered as: ', M );
  95. M := Factorial( M );
  96. END; { ELSE }
  97. WriteLn;
  98. WriteLn(' ':20,' M!, or M Factorial = ', M );
  99. WriteLn;
  100. WriteLn(' Press <ENTER> to return to menu.');
  101. ReadLn;
  102. END; {FactRec}
  103.  
  104. {------------------------------------}
  105. { MAIN PROGRAM }
  106. {------------------------------------}
  107. BEGIN
  108. Init;
  109. UserQuits := False;
  110. REPEAT
  111. ClrScr;
  112. WriteLn;
  113. WriteLn(' ':25, ' Menu For Factorial Methods ' );
  114. WriteLn;
  115. WriteLn(' ':20, ' 1. Factorial Using a FOR DO Loop' );
  116. WriteLn(' ':20, ' 2. Factorial Using Recursion' );
  117. WriteLn(' ':20, ' 3. Quit ' );
  118. ReadLn ( Choice );
  119. CASE Choice OF
  120. 1 : FactForDo;
  121. 2 : FactRec;
  122. 3 : UserQuits := True;
  123. END;
  124. UNTIL UserQuits;
  125. END.
Real Eyes Realize Real Lies
My Resume
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 1
Reputation: still student is an unknown quantity at this point 
Solved Threads: 0
still student still student is offline Offline
Newbie Poster

veeeeeeery Urgent

 
0
  #7
Mar 30th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC