Factorial problem

Hey guys,
I'm making a program that takes an input and chucks out the factorial result.
Now the program just chucks out what i put into it when i input 5 when I'm pretty sure it should output 120.
If anyone can point out where im going wrong it'd be appreciated.
code below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>;
using namespace std;
class Factorial
{
private:
	int number; 
	//int temp;
	int resultNumber;
public:
	//int getNumber() {return number;}
	//int getResult() {return resultNumber;}
	Factorial(int numberInput) : number(numberInput)
	{
	}
	~Factorial()
	{
	}
	void setNumber(int numberInput) {number = numberInput; return;}

	int calculation(int numberInput) 
	{ 
		resultNumber =number;
		for(; number<=1; number--)
		{
			resultNumber = resultNumber *1;
		}
		cout << "Factorial of number number input is " << resultNumber;
		return resultNumber;
    } 
	

};
int main()
{
	int numberInput;
	//void setResult(int resultNumber) {result = resultNumber; return;)
	cout << "Please enter a number";
	cin >> numberInput;
	/*for(numberInput; numberinput > 0;numberInput--)
	{
		result = result *numberInput;
	}*/
	Factorial f1(numberInput);
	f1.calculation(numberInput);

	cin.ignore(2);
}
Something wrong here:
for(; number<=1; number--)
i changed the resultNumber =number to resultNumber =1 then changed resultNumber = resultNumber * 1 to resultNumber = resultNumber * number as that made more sense but now its just chucking out 0
sorry lowest one didnt see your reply but i already declared number before that and as far as i know i still have to put the ; there even if nothings going in it.
the number going in should be 5 then it gets reduced by 1 per iteration until it becomes less then 1 or equal to it which then stops the for loop

Or thats whats supposed to happen in my mind i could be wrong.
I didn't lie to you about something being wrong there :)

What makes your loop stop?
Last edited on
my loop should stop when number gets reduced to either 1 or less then 1.
What makes this stop:
for(; number<=1; number--)
ahh took a while but it finally hit me i swapped the < to a > and changed the cout line to output resultNumber instead of temp.
It now works properly thanks alot for the help.

silly mistakes make me feel stupid but least i learn from them.
Last edited on
hey just one last thing if i input 20 then it outputs a -number which im guessing is because of the size limit of an int so i tried putting long int for resultInteger instead but its still doing it.
Use unsigned long long for maximum storage capacity, any larger than that and you'll have to look into either making your own way to handle larger numbers or finding libraries to do it for you. Long can map to the same type as int, which it sounds like it did in your case.
thank you that solved that little problem :P ive done factorial through recursion now aswell which didnt take aslong as i thought it would. having problem with it calling my functions through ive tried both switch and if statements. when i try to launch a function with the if statements it works fine but if i try to make it call them through an if or switch it just skips them completely.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>;
using namespace std;
class Factorial
{
private:
	int number; 
	long long resultNumber;
public:
	//int getResult() {return resultNumber;}
	Factorial(int numberInput) : number(numberInput)
	{
	}
	~Factorial()
	{
	}
	void setNumber(int numberInput) {number = numberInput; return;}

	long long iterativeCalculation(int numberInput)
	{
		resultNumber =1;
		for(; number>=1; number--)
		{
			resultNumber = resultNumber *number;
		}
		cout << "Iterative Factorial of number input is \n" << resultNumber;
		return resultNumber;
    } 
	int recursiveCalculation(int number)
	{
		if (number <= 1) 
		{ 
			return 1; 
		}
		else 
		{ 
			return (number * recursiveCalculation(number-1)); 
		}
    }
};
int main()
{
	char type;
	int numberInput;
	cout << "Please enter a number \n";
	cin >> numberInput;
	cout << "Please choose either iterative or recursive factorial \n";
	cin >> type;
	if (type == 1)
	{
		Factorial f1(numberInput);
		f1.iterativeCalculation(numberInput);
		cout << endl;
	}
	if(type == 2)
	{
		Factorial f2(numberInput);
		cout << f2.recursiveCalculation(numberInput);
		cout << endl;
	}
	cin.ignore(2);

}


btw i know ive said it before but thanks again guys for this you've helped me alot :P
type is a character, and I'm not sure what you would have to hit for it to equal 1. Compare against the character value for 1:
if (type == '1') //...
You can do it with a simple recursive function, as you can see in the post belw. I don't know why you need a class.
oh damn forgot the ' '.
it has to hit one so the user can make a selection between either iterative or recursive and using if statements to allow for the option seemed like the easiest way as switch seemed excessive for only two options.

also cosmin i need a class as its part of the work that we need to make a separate class that has an iterative function and recursive which work out the factorial.

thanks again for the help.
Last edited on
Topic archived. No new replies allowed.