answer gives wrong

Dec 28, 2018 at 5:30pm
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 Dec 28, 2018 at 5:35pm
Dec 28, 2018 at 6:55pm
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 Dec 28, 2018 at 6:56pm
Dec 28, 2018 at 7:00pm
1!+2!+3!+4!+5! takes Factorial number
Last edited on Dec 28, 2018 at 7:05pm
Dec 28, 2018 at 7:16pm
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;
}
Dec 28, 2018 at 7:29pm
Ok!
Topic archived. No new replies allowed.