943,509 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6996
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 23rd, 2003
0

Factorial?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fastcarz3 is offline Offline
7 posts
since Feb 2003
Feb 23rd, 2003
0
Re: Factorial?
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Feb 23rd, 2003
0
Re: Factorial?
na doesn't work
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fastcarz3 is offline Offline
7 posts
since Feb 2003
Feb 23rd, 2003
0
Re: Factorial?
What output does it give?
Try int x;
int factorial=1; on two separate lines?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Feb 23rd, 2003
1
Re: Factorial?
Oops! Just realized
the last line should cout << factorial
not cout << x

edited post above to fix error
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Feb 23rd, 2003
0
Re: Factorial?
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)


???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fastcarz3 is offline Offline
7 posts
since Feb 2003
Feb 23rd, 2003
0
Re: Factorial?
Make sure your whole program is enclosed in

C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2.  
  3. int main()
  4. {
  5.  
  6. [ CODE GOES HERE ]
  7.  
  8. return (0);
  9. }
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Feb 23rd, 2003
0
Re: Factorial?
#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;
}


???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fastcarz3 is offline Offline
7 posts
since Feb 2003
Feb 23rd, 2003
0
Re: Factorial?
You're missing a } to end the if statement.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Feb 24th, 2003
0

Re: ?

Quote 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.
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003

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: Problem with basic templates on GCC
Next Thread in C++ Forum Timeline: Recursion





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


Follow us on Twitter


© 2011 DaniWeb® LLC