factorial program

The factorial of a non-negative integer n (written n! and read n factorial) is defined by
n! = 1 ,if n = 0 or 1
= 1*2*3*....n, if n > 1
So for instance, 3! = 1* 2 * 3 = 6 and 5! = 1 * 2 * 3 * 4 * 5 = 120 etc.
The number e the natural base of the exponential function is defined as
e = 1/0! + 1/1! + 1/2! + 1/3! + ....1/n!+ .....
Write a program that estimates the value of e using the first few terms up to 1/n!, that is
e = 1/0! + 1/1! + 1/2! + 1/3! + ....1/n!
The user is asked for the value of n, then compute the above sum.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main() {

	int n, i;
	long nfact=1;
	cout << "n=";
	cin >>n;
	if ( n >0 )
	for ( i = 1;i <=n;i++)
	nfact += 1.0/i // im confused here.
	cout << "n!= "<< nfact;
	


this is all i got so far or is my code entirely wrong ?
Last edited on
Well you have the basic ideas for a factorial

1
2
3
4
5
6
7
8
if( n > 0 )
{
    factorial = 1;
    for( int i = 0; i < n; ++i )
    {
        factorial *= i + 1;
    }
}


How ever you are not understanding the formula for e

it is e = 1/0! + 1/1! .... 1/n!

so say for example we have n = 5 this means:
e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + 1/5!
e = 1/0 + 1/1 + 1/2 + 1/6 + 1/24 + 1/120

Basically when they are in order like that you can just multiply the denom by the next value


Basically it would then we could say this

1
2
3
4
5
6
7
double e = 0; //same as 1/0!
int denominator = 1;

for( int i = 0; i < n; ++i )
{
    e += 1.0 / ( denominator *= i + 1 );
}


I think that is correct I haven't tested I am busy right now sorry but if it's not exactly correct I'll fix it when I have more time.

*edit had a typo with my 5! it was 120 not 60
Last edited on
I'm not focusing on the logic of the problem but your syntax is off

if/else conditions that have more than one statement require braces
your for loop should also have braces just for good practice

also you should indent after the initial if-conditional and for-loop statement to make it clearer which statements belong to whatever your doing
Last edited on
You need braces for your if statement and your for loop. otherwise, this won't work.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main() {
	
	int n,i;
	cout << " Enter n=";
	cin >> n;
	
	double e=0;
	int denominator=1;

		for (int i=0; i < n;i++)
	{
	e+=1.0/ (denominator *= i+1 );
	cout <<"The sum is " <<e;
	}
	return 0;
}


i dont think the code is right cuz i keep getting numbers that are bigger than what it's supposed to be ( type in n=2, turns out to be 11.5)
and i keep getting "The sum is" multiple times.
Nope I just checked my formula is correct ( according to what he said ) except for one thing. He gave me the wrong formula he said the first value was 1/0! and idk for some reason I was thinking it was = to 0 but thats undefined..the real formula for find e is 1 + 1/1 , + 1/2 + 1/3 + 1/n.. The reason you got 11.5 for 2 idk you must have outputted a one first on accident.


I fixed the formula by changing intial value of e to 1 and I moved the denominator *= outside of the e += to make it look neater.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
	const int n = 20;
	double e = 1.0;
	int denominator = 1;

	for( int i = 0; i < n; ++i )
	{
		denominator *= i + 1;
    	        e += 1.0 / denominator;
	}
	std::cout << e << std::endl;
}


Cheers.

Oh and you are getting the sum is multiple times because you have it inside of the for loop.
Last edited on
Purely for reasons of style, I'd prefer to avoid the i + 1 - can't explain why, it is just not aesthetically pleasing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    const int n = 20;
    long double e = 1.0L;
    long double denominator = 1.0L;
    
    for (int i = 1; i <= n; ++i )
    {
        denominator *= i;
        e += 1.0L / denominator;
    }
    std::cout.precision(20);
    std::cout << e << std::endl;
}
when i enter 2 for n, is it suppose to print 2 and 2.5?? isnt it suppose to just print 2.5?
Last edited on
Are you sure the first 2 isn't the input?

*edit
It should output 2.5 since 1 + 1/1 + 1/2 = 1 + 1 + .5 = 2 + .5 = 2.5

I see your problem maybe it's because you have the n = ... output and never put an endl;
Last edited on
when i put endl after it it adds another 2....
i think its easier if u just look at my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main() {
	
	int n,i;
	cout << "Enter n=";
	cin >> n;
	double e=1.0;
	int denominator=1;
		for (int i=0; i < n;i++)
	{
	e+=1.0/ (denominator *= i+1 );
	cout << e<<endl;
	}
	return 0;
}



ok it worked out fine somehow when i put the cout out of the loop and deleted all the endl;
Do you know why it works now without all the endl?
Last edited on
because you have the output inside of the for loop switch line 14 and 15
Topic archived. No new replies allowed.