series problem stucked in the last step

This is a discussion on series problem stucked in the last step within the C Programming forums, part of the General Programming Boards category; I am facing some problems sloving this problem... Code: sum (2^i * i! ) that means user inputs an number ...

1. series problem stucked in the last step

I am facing some problems sloving this problem...

Code:
`sum (2^i * i! )`
that means user inputs an number i then the program will calculate 2^i*i! and add the previous i values...

This should be like this
Code:
`2^1* 1! + 2^2*2! + 2^3*3! +........+2^n*n!`

i am almost solve to the third steps that is calculating ( 2^i * i ! )

but in the last step i cannot add the previous values..i know what to do to get the result that i will need a variable x initialised to 0(zero) and which will increase until it is equal to i .
that is
Code:
```for (x=0;x <= i; i++)
{
x+= (previous value);
}```
But i donot know where to put this and i donot know how to make a fuction with this loop..please help me out..i am pasting my existing code that is calculating up to (2^i * i! )

Code:
```#include <stdio.h>
#include <conio.h>

int factorial(int i);
int power( int base, int exp );

main()
{
int n,i,b=2,s=0,multiply;
scanf("%d", &i);
for (n=1;n<=i;n++)
{
s=((int) factorial(n));
}
multiply = s*power( b, i );
printf ("The Factorial of %d is\t     : %d\n2 to The Power of %d is\t     : %d\n(Factorial x Exponent) is    : %d\n",i, s,i, power( b, i ), multiply);
getch();
}

int factorial(int i)
{
if (i<=1)
return(1);
else
i=i*factorial(i-1);
return i;
}
int power( int base, int exp )
{
int i, p = 1;
for( i = 1; i <= exp; i++ )
{
p *= base;
}
return p;
}```

2. What your looking at is something like this

Code:
```	for(n=1;n<=i;n++)
{
s = factorial(n);

multiply = s* pow( b, n );

printf ("The Factorial of %d is\t     : %d\n\
2 to The Power of %d is\t     : %d\n\
(Factorial x Exponent) is    : %d\n",i, s,i, power( b, i ), multiply);
}

/* my output
2
The Factorial of 2 is        : 1
2 to The Power of 2 is      : 4
(Factorial x Exponent) is    : 2
The Factorial of 2 is        : 2
2 to The Power of 2 is      : 4
(Factorial x Exponent) is    : 8

*/```
I could also see few other things which are you will have to improve in your coding practice. But if you get your head around why your didn’t work, perhaps i shall post the other. Now tell me why dint your work? Compare it with mine and yours.

ssharish

3. Code:
```int sum = 0;    // cannot hold value more than INT_MAX defined in limits.h
int i ;
// sum ( 2 ^ i * i! )
for(i = 1; i <= n; i++) {
sum += power(2,i) * factorial( i );
// sum = sum + power(2,i) * factorial( i );
}```
i,j,k are usually used for counter. Take note that for large value of n, integer may not be big enough.

4. Your thinking is wrong here:
Code:
```for (x=0;x <= i; i++)
{
x+= (previous value);
}```
You don't add the previous value to x.
x already holds the partial sum of all the previous values, and here you now add the current term to it.