Find out the smallest integer whose factorial is greater than 3000.

Oct 26, 2014 at 10:32pm
Hi guys, I'm just working on an assignment and I have no idea where to start. I'm just starting to learn for loops and while loops, can this program be accomplished by nesting one inside the other? I'm not asking anyone to do the assignment for me, I just really don't know where to start.

Thanks, Jeremy.
Oct 26, 2014 at 10:40pm
Algo:
1
2
3
4
from 1 to 3000
	-if(factorial(i) > 3000)
		-break;
anwer = i
Oct 26, 2014 at 10:55pm
I appreciate the help but I would have to do it using a loop of some sort and we havent learned factorial() so it would have to be done using a for loop, a do while loop or just a while loop...
Last edited on Oct 26, 2014 at 10:55pm
Oct 26, 2014 at 11:24pm
closed account (48T7M4Gy)
First of all do you know what a factorial is?

http://www.mathsisfun.com/numbers/factorial.html
Last edited on Oct 26, 2014 at 11:24pm
Oct 26, 2014 at 11:26pm
Yes I do. It would need to be something similar to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int factorial(int n){

	int i = 0;
	int	fact = 1;

	{
		for (i = 1; i <= n; i++){

			fact = fact*i;

		}

		return(fact);

	}

}
Oct 26, 2014 at 11:32pm
closed account (48T7M4Gy)
Looks OK so the next step is to use that to find the largest value of n which doesn't give a higher factorial than 3000. Maybe loop through n's until you find it? Start at n=1 and keep incrementing n until factorial(n) >= 3000?
Oct 27, 2014 at 12:42am
Okay I fixed it.

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

//Lab 3-3: Find the smallest integer whose factorial is greater than 3000

#include<iostream>
using namespace std;

int factorial(int n);

int main()

{

	int fact;



	int n = 1;

	do{	n++;

		fact = factorial(n);
	}
	while (fact <= 3000);

	cout << "The smallest integer whose factorial is greater than 3000 is " <<
		n << " and its factorial is "<< fact <<"."<<endl;
		

	//end program
	cin.get();
	return(0);



}

int factorial(int n){

	int i = 0;
	int	fact = 1;

	{
		for (i = 1; i <= n; i++){

			fact = fact*i;

		}

		return(fact);

	}

}
Oct 27, 2014 at 12:45am
closed account (48T7M4Gy)
Excellent :)
Topic archived. No new replies allowed.