answer gives wrong

i probably solved but in last some problem and gives wrong answer please indicate the mistake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
main()
{
	int i,j;
	float f,sum;
	sum=0;
	for(i=1;i<=5;i++)
	{
		f=1;
		for(j=1;j<=i;j++)
		f=f*j;
		sum=sum+f;
	}
	cout<<"Sum of series:"<<sum;
}
Last edited on
What do you want it to do exactly? Also I would declare f=1; outside of your for loop unless you want it to reset to 1 every time the i for loop increments.
Last edited on
1!+2!+3!+4!+5! takes Factorial number
Last edited on
If youre doing factorials and your multiplying by the value in the for loop you should decrement instead. The best way to solve factorials is with recursion. So much easier. Here is an example:

1
2
3
4
5
6
7
int factorial(int n)
{
    if(n > 1)
        return n * factorial(n - 1);
    else
        return 1;
}
Ok!
Topic archived. No new replies allowed.