### While Loop Code HELP ME!!

Hi Everyone! Can you help me do this program because i am a beginner. =)
Here is the sample output:

Enter Number: 5
Solution:
5 x 4 x 3 x 2 x 1
Result is 120

Using WHILE LOOP CODE!!

TY IN ADVANCE TO THOSE WHO WILL HELP ME! :)
 ``123456789101112131415`` ``````#include int main() { int number; std::cout << "Enter Number" << std::endl; std::cin >> number; int product=1; // set to 1 since we're multiplying while (number > 0) { product*=number // this multiplies the current product by number; number--; // this subtracts number by 1; } std::cout << product << std::endl; }``````
Last edited on
you may call this overloaded function:

 ``12345`` ``````long int factorial(int x) { if (x==0) return 0; else if (x==1) return 1; else return x * factorial(x-1);} ``````

Last edited on
that's a recursive function. not an overloaded one.
I stand corrected but what is more important is what it does and not what it is classified.
0! is 1

Also, what does your function do in this case?

`int res = factorial(-3);`

Andy

PS http://en.wikipedia.org/wiki/Factorial
 The value of 0! is 1, according to the convention for an empty product.
Last edited on
 ``1234567891011121314151617181920212223242526272829`` ``````#include #include #include #include #include int main() { while ( true ) { std::cout << "Enter a non-negative number (0 - exit): "; unsigned int n = 0; std::cin >> n; if ( !n ) break; std::vector v( n ); std::iota( v.begin(), v.end(), 1 ); std::cout << "\nTable of factorials for n = " << n << std::endl; std::partial_sum( v.begin(), v.end(), std::ostream_iterator( std::cout, "\n" ), std::multiplies() ); std::cout << std::endl; } }``````
Last edited on
lol vlad. You're funny, and I approve.
Topic archived. No new replies allowed.