943,600 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 89808
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 19th, 2004
0

factorial using a for loop

Expand Post »
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!


C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2.  
  3.  
  4. #define q = 3
  5.  
  6. int main()
  7.  
  8.  
  9. {
  10. int n;
  11. if (n < 0) return 0;
  12. int f;
  13.  
  14. for (n=1; n <= q; n++)
  15. {
  16. double f = 1;
  17. {f *= n;}
  18. }
  19.  
  20. cout<< f;
  21. char w;
  22. cin>>w;
  23. return 0;
  24. }
Last edited by Nick Evan; Jan 27th, 2010 at 11:03 am. Reason: Added code-tags
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
ellisrn is offline Offline
6 posts
since Jul 2004
Jul 19th, 2004
1

Re: factorial using a for loop

The following header is deprected, instead try to use <iostream>
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
You shouldn't have the equals sign here.
C++ Syntax (Toggle Plain Text)
  1. #define q = 3
I have no idea what this is intended to do.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 19th, 2004
0

Re: factorial using a for loop

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




Quote originally posted by Dave Sinkula ...
The following header is deprected, instead try to use <iostream>
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
You shouldn't have the equals sign here.
C++ Syntax (Toggle Plain Text)
  1. #define q = 3
I have no idea what this is intended to do.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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. */
Reputation Points: 11
Solved Threads: 0
Newbie Poster
ellisrn is offline Offline
6 posts
since Jul 2004
Jul 20th, 2004
0

Re: factorial using a for loop

see this example dude
C++ Syntax (Toggle Plain Text)
  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. }
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
Jul 20th, 2004
-1

Re: factorial using a for loop

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>>
Last edited by Nick Evan; Jan 27th, 2010 at 11:04 am. Reason: removed mail
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahsn is offline Offline
1 posts
since Jul 2004
Jul 20th, 2004
-2

Re: factorial using a for loop

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
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
Mar 30th, 2009
-1

veeeeeeery Urgent

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
still student is offline Offline
1 posts
since Mar 2009
Jan 27th, 2010
-2

what about 100! ?

The following header is deprected, instead try to use <iostream>
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
You shouldn't have the equals sign here.
C++ Syntax (Toggle Plain Text)
  1. #define q = 3
I have no idea what this is intended to do.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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. */
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?
Reputation Points: 8
Solved Threads: 0
Newbie Poster
angieng is offline Offline
1 posts
since Jan 2010
Jan 27th, 2010
0
Re: factorial using a for loop
You cant use int for such big numbers as 100!, since int usually can handle 32bit value.
solution is google: "c++ big integer"
Reputation Points: 11
Solved Threads: 17
Junior Poster in Training
pecet is offline Offline
95 posts
since Oct 2009
Jan 27th, 2010
0
Re: factorial using a for loop
Click to Expand / Collapse  Quote originally posted by ellisrn ...
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
C++ Syntax (Toggle Plain Text)
  1. nt numbers[50];//setup array
  2. numbers[0]=1; //factorial of 0 is 1
  3. int factorial = 0;//initialize your factorial
  4.  
  5. //loop will multiply all the numbers in the array
  6. for (int i = 1; i < 50; i ++){
  7. factorial = numbers[i] * numbers[i -1];
  8.  
  9. }
  10.  
  11. 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[50] starts as array[0] and finishes as array[49];

Cheers.
Last edited by Nick Evan; Jan 27th, 2010 at 11:02 am. Reason: Added code-tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akvino is offline Offline
3 posts
since Jan 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Retrive data from the recve() function
Next Thread in C++ Forum Timeline: Single value





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC