Factorial?

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Feb 2003
Posts: 7
Reputation: fastcarz3 is an unknown quantity at this point 
Solved Threads: 0
fastcarz3 fastcarz3 is offline Offline
Newbie Poster

Factorial?

 
0
  #1
Feb 23rd, 2003
can anyone come up with a solution for this ? real quick for me?

the factorial of a nonnegative integer n is written n! ("n factorial") and is defined as follows: n!=n*(n-1)*(n-2)*.....*1(for values of n greater than to 1)
and n!=1 (for n=0 or n=1).

For example, 5!=5*4*3*2*1, which is 120. Use a while structure for the following..write a program that reads a nonnegative integer and computes and prints its factorial.

If anyone could this for me thanks.
Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
 
0
  #2
Feb 23rd, 2003
  1. int x, factorial = 1;
  2. cout << "Enter integer: ";
  3. cin >> x;
  4. if (x > 0)
  5. {
  6. while (x > 0)
  7. {
  8. factorial *= x;
  9. x--;
  10. }
  11. }
  12. cout << "Factorial: " << factorial;

Untested, this might work.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Quick reply to this message  
Join Date: Feb 2003
Posts: 7
Reputation: fastcarz3 is an unknown quantity at this point 
Solved Threads: 0
fastcarz3 fastcarz3 is offline Offline
Newbie Poster
 
0
  #3
Feb 23rd, 2003
na doesn't work
Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
 
0
  #4
Feb 23rd, 2003
What output does it give?
Try int x;
int factorial=1; on two separate lines?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
 
1
  #5
Feb 23rd, 2003
Oops! Just realized
the last line should cout << factorial
not cout << x

edited post above to fix error
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Quick reply to this message  
Join Date: Feb 2003
Posts: 7
Reputation: fastcarz3 is an unknown quantity at this point 
Solved Threads: 0
fastcarz3 fastcarz3 is offline Offline
Newbie Poster
 
0
  #6
Feb 23rd, 2003
Compiling...
3.cpp
C:\c++ storage\3.cpp(7) : error C2143: syntax error : missing ';' before '<<'
C:\c++ storage\3.cpp(7) : error C2501: 'cout' : missing storage-class or type specifiers
C:\c++ storage\3.cpp(7) : error C2143: syntax error : missing ';' before '<<'
C:\c++ storage\3.cpp(9) : error C2143: syntax error : missing ';' before '>>'
C:\c++ storage\3.cpp(9) : error C2501: 'cin' : missing storage-class or type specifiers
C:\c++ storage\3.cpp(9) : error C2143: syntax error : missing ';' before '>>'
C:\c++ storage\3.cpp(12) : error C2143: syntax error : missing ';' before 'if'
C:\c++ storage\3.cpp(13) : error C2143: syntax error : missing ';' before '{'
C:\c++ storage\3.cpp(13) : error C2447: missing function header (old-style formal list?)
C:\c++ storage\3.cpp(20) : error C2143: syntax error : missing ';' before '<<'
C:\c++ storage\3.cpp(20) : error C2501: 'cout' : missing storage-class or type specifiers
C:\c++ storage\3.cpp(20) : error C2086: 'cout' : redefinition
C:\c++ storage\3.cpp(20) : error C2143: syntax error : missing ';' before '<<'
Error executing cl.exe.

3.obj - 13 error(s), 0 warning(s)


???
Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
 
0
  #7
Feb 23rd, 2003
Make sure your whole program is enclosed in

  1. #include<iostream.h>
  2.  
  3. int main()
  4. {
  5.  
  6. [ CODE GOES HERE ]
  7.  
  8. return (0);
  9. }
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Quick reply to this message  
Join Date: Feb 2003
Posts: 7
Reputation: fastcarz3 is an unknown quantity at this point 
Solved Threads: 0
fastcarz3 fastcarz3 is offline Offline
Newbie Poster
 
0
  #8
Feb 23rd, 2003
#include <iostream>

int main()
{


int x, factorial = 1;


cout << "Enter integer: ";

cin >> x;


if (x > 0)
{
while (x > 0)
{
factorial *= x;
x--;
}

cout << "Factorial: " << factorial;


return 0;
}


???
Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
 
0
  #9
Feb 23rd, 2003
You're missing a } to end the if statement.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member

Re: ?

 
0
  #10
Feb 24th, 2003
Originally Posted by fastcarz3
can anyone come up with a solution for this ? real quick for me?

If anyone could this for me thanks.
That's the second piece of homework you've posted here asking for a solution, with no attempt on your own part to work it out yourself. And that's after I spent time to give you an approach to problem solving yesterday.

If you can't do the course go find something more suitable.
Quick reply to this message  
Closed Thread

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