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.

Recommended Answers

All 19 Replies

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.

na doesn't work

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

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

edited post above to fix error

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)


???

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

???

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

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.

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

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. :)

that was my point *exactly*!!!

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

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

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 an approach 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.

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

There's always hope ;-)

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:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.