Display the output: Number Factorial 1 1 2 2 3 6 4 24 5 120 pplx can some 1 write the code 4 me in C++
How far did you get with it and what gave you trouble?
@e.mccormick non
Well, why are you having problems making code to do this?
cox i missed the class i was ill that day
Well, what was the class on? Which aspect of C++? Loops? Recursion?
i think so it was on loops
OK. First off, do you know what a factorial is?
u have to display this out put on the screen yup i know that 1! is 1 5! is 1.2.3.4.5
@e.mccormick plx help me
Good, good. OK. So, the idea of the loop os to do the repetitive work. As you say there, the 5! is 1 times 2 times 3 times 4 times 5. But, what if your program is given some input x. Then how to tell the program to do x! as an operation. That is where the loop comes in. It does the repetitive work.
i dnt know that thats what i am asking
There are two basic types of loops. One is the for loop, which does things for a specific set of items or number of times. The other is the while loop, which does things while something is true. There are some variants, but you can think about them as basically being those two things.
ahan right
This is an overview of loops: http://www.tutorialspoint.com/cplusplus/cpp_loop_types.htm But down a bit you can see they have the for loop in the loop type list: http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm At the bottom of that page there is this example: ``` #include <iostream> using namespace std; int main () { // for loop execution for( int a = 10; a < 20; a = a + 1 ) { cout << "value of a: " << a << endl; } return 0; } ``` Then they show the output as this: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 As you can see, it started with 10 then counted to 19. This is all controled by the for statment: for( int a = 10; a < 20; a = a + 1 ) for( ) is telling the compiler it is a for loop. int a = 10; is the initial state. It makes the variable a as an integer type and sets it to 10. a < 20; This is the condition part. It means "While a is less than 20, do this loop". a = a + 1 Is what it does at the end of each pass of the loop. So it starts with 10. Prints it as 10, set it to 11. 11 is less than 20 so it goes again. It keeps doing this until it hits 20, then the loop stops and it never prints 20. That is one important concept. Where it starts and stops and why:
ahan
because you want to do a factorial you will want something in the loop to do the work, simple multiplication, but you will also want something outside the loop to store the result. See, when you do that variable in the loop, the scope is in the loop. Once the loop ends, the variable goes away. So, if I did this: ``` #include <iostream> using namespace std; int main () { // for loop execution for( int a = 10; a < 20; a = a + 1 ) { cout << "value of a: " << a << endl; } cout << "value of a: " << a << endl; return 0; } ``` See the second cout << "value of a: " << a << endl;? It is outside the loop! So that would give an error because a is not declared outside the loop!
But lets say I did this: ``` #include <iostream> using namespace std; int main () { int b = 0; // for loop execution for( int a = 10; a < 20; a = a + 1 ) { cout << "value of a: " << a << endl; b+=a; } cout << "sum of all a values: " << b << endl; return 0; } ``` Now I am countin a inside the loop and adding a to b inside the loop. Then, after the loop is done, I print out the sum I made. This would work! This is also the foundation of how you want to do your factorial.
ahan
So, can you see how you could multiply using something like that?
yup right
Well, give it a try and see what you get. Just one hint. I did my outside variable as b=0 because I was doing addition. Starting with 0 for addition is important because it does not change the result. But you are doing multiplication! So your factorial saving variable must start as 1 so that it does not change the end result.
thnk u
And if you have trouble, post the code here. Just put a ``` above and below the code block. That is how I did the code highlighting. (It is the one with the ~ on the same key.) If you don't, it can't be copied or easily read...
its not working for factorilal
post your code...
Yes, we need to see what you did and how you are deciding what numbers. Personally, I would use a count down loop so that it starts with the number, multiplies it into the factorial, counts down by 1, and keeps repeating while the value is > 1.
Join our real-time social learning platform and learn together with your friends!