954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Factorial?

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.

fastcarz3
Newbie Poster
7 posts since Feb 2003
Reputation Points: 10
Solved Threads: 0
 
int x, factorial = 1;
cout << "Enter integer&#58; ";
cin >> x;
if &#40;x > 0&#41;
&#123;
     while &#40;x > 0&#41;
     &#123;
          factorial *= x;
          x--;
     &#125;
&#125;
cout << "Factorial&#58; " << factorial;


Untested, this might work.

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

na doesn't work

fastcarz3
Newbie Poster
7 posts since Feb 2003
Reputation Points: 10
Solved Threads: 0
 

What output does it give?
Try int x;
int factorial=1; on two separate lines?

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

Oops! Just realized
the last line should cout << factorial
not cout << x

edited post above to fix error

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

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)


???

fastcarz3
Newbie Poster
7 posts since Feb 2003
Reputation Points: 10
Solved Threads: 0
 

Make sure your whole program is enclosed in

#include<iostream.h>

int main&#40;&#41;
&#123;

     &#91; CODE GOES HERE &#93;

     return &#40;0&#41;;
&#125;
cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

#include

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;
}


???

fastcarz3
Newbie Poster
7 posts since Feb 2003
Reputation Points: 10
Solved Threads: 0
 

You're missing a } to end the if statement.

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

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

Pardon me, Bob, but don't you think a pm on that would have been more appropriate?

aeinstein
Team Member - aka kaynine
Team Colleague
645 posts since May 2002
Reputation Points: 186
Solved Threads: 8
 

Pardon me, aeinstein, but don't you think a pm on that would have been more appropriate? ;)

I'm just joking. Ok now someone say it back to me. :)

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

that was my point *exactly*!!!

aeinstein
Team Member - aka kaynine
Team Colleague
645 posts since May 2002
Reputation Points: 186
Solved Threads: 8
 

well bob its a required course so if i make it through you'll never hear from me again until then i need some help and answers

fastcarz3
Newbie Poster
7 posts since Feb 2003
Reputation Points: 10
Solved Threads: 0
 
well bob its a required course so if i make it through you'll never hear from me again until then i need some help and answers


LOL! OK, with an attitude like that, just what CENTURY do you think it's going to be before Bob helps you with an assignment again? ROFLOL! :D

aeinstein
Team Member - aka kaynine
Team Colleague
645 posts since May 2002
Reputation Points: 186
Solved Threads: 8
 
well bob its a required course so if i make it through you'll never hear from me again until then i need some help and answers


It seems to me that you're after answers rather than help. Providing answers isn't necessarily helping you.

I spent time replying to your previous question, providing you with anapproach to solving problems such as these. In doing so, I was trying to help you to find answers yourself, to that and other problems, rather than just give you the answer.

Perhaps you didn't find that reply useful; perhaps time constraints mean you'd rather just pick up the code and move on. It's up to you. But if you're going to be solving more programming problems of a similar nature then at some stage, whether now or later, it will probably be worth your while going back and reading what I said previously so that you can develop an approach that will help you. That way you'll be less dependent on other people. And you'll be able to come back and ask for help rather than answers.

Good luck.

Bob
Junior Poster
Team Colleague
129 posts since Feb 2003
Reputation Points: 15
Solved Threads: 2
 

Look's like I stand corrected - pleasantly surprised you to took the time to make another reply Bob.

aeinstein
Team Member - aka kaynine
Team Colleague
645 posts since May 2002
Reputation Points: 186
Solved Threads: 8
 

There's always hope ;-)

Bob
Junior Poster
Team Colleague
129 posts since Feb 2003
Reputation Points: 15
Solved Threads: 2
 

he ha ha ha ha ha ah aha aha aha aha haa ha ha ha ha ha ha aha ha aha ahja aha ha ah 11 :rolleyes: :p :lol: :mad: :( :mrgreen:

guest
Newbie Poster
19 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
he ha ha ha ha ha ah aha aha aha aha haa ha ha ha ha ha ha aha ha aha ahja aha ha ah 11 :rolleyes: :p :lol: :mad: :( :mrgreen:


Please refrain from bumping two year old threads.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You