Trouble combining concepts (Function+Do while loop to get factorial)

My code is below. I tried to use a function and a do-while loop to find factorials, but I'm having some difficulties in doing so. I'm using Code::Blocks on Ubuntu, but for what it's worth, my opinion is that I'm using the code poorly.
Also, I could be misunderstanding the concepts entirely. Please let me know if you see what's wrong with it, whether that be syntax or usage. (Or both.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// To find a factorial, compile and input a number.

#include <iostream>
using namespace std;
int multiply (int a, int b)
{
    int c;
    c = a + b;
    return c;
}

int main()
{
    int input;
    int answer;
    cout<<"To find the factorial of a number, please enter one and press enter.";
    cin>>input;
    do {
        answer = multiply (input, (input - 1));
        while (input > 0);
        cout<<answer;
    }
}
Last edited on
I am new myself so take this for what it is worth. I don't see a reason to use a loop. It looks like you are just trying to get a single result. If that is the case, this is what I came up with to get your function to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int multiply (int a, int b)
{
    int c;
    c = a + b;
    return c;
}

int main()
{
    int input=0;
    int answer=0;
    cout<<"To find the factorial of a number, please enter one and press enter."<<endl;
    cin>>input;
	cout << endl;
    
	answer = multiply (input, (input - 1));
	
	cout<<answer<<endl;

	system ("pause");
	return 0;
}
1
2
3
4
5
6
int multiply (int a, int b)
{
    int c;
    c = a + b;
    return c;
}

This function does addition, not multiplication.

1
2
3
4
5
do {
        answer = multiply (input, (input - 1));
        while (input > 0);
        cout<<answer;
}

This code does not compile. The "while" condition should be just after the do { ... } block. Another thing: input remains unchanged so it will never reach zero unless the user enters zero. You should call input--; at the end of the loop.

Now, answer would not store the factorial of input at the end of the loop even with those changes. At every iteration, you're just computing the product of input and input - 1. What you need to do is start off answer as the initial value of input. Then, at every iteration multiply answer by input - 1 (and remember to decrement input).
I see what you are doing now. The problem I am running into is the looping equation to use. I don't think you need the function.

the problem is the start of the loop. you have started with say 5 and 4 when you enter 5. now you need to take the result of 5 and 4 and multiple it by 3 and then take that result by 2 until 1. I can't quite get the looping equation done.

a for loop may be better by the equation needs work. sorry I can't be any more help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    int input=0;
    int answer=1;
    cout<<"To find the factorial of a number, please enter one and press enter."<<endl;
    cin>>input;
	cout << endl;
    
	for (int i=1; i <=input; i++)
	{
		TBD;
	}
		
	cout<<answer<<endl;

	system ("pause");
	return 0;
}
It ain't pretty but is works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
    int input=0;
    int answer=1;
    cout<<"To find the factorial of a number, please enter one and press enter."<<endl;
    cin>>input;
	input = input + 1;
	cout << endl;
    
	for (int i=1; i <=input-1; i++)
	{
		answer = answer * (input-i);
	}
		
	cout<<answer<<endl;

	system ("pause");
	return 0;
}
Topic archived. No new replies allowed.