Beyond Verses

My blog that specialize in Space Science and Latest news from NASA

Thursday, September 25, 2008

C Solve Problem

// s = 1- 2x + 3xpow2 - 4xpow3 + .........
#include <stdio.h>
#include <math.h>
void main( )
{
float x,s,r ;
int a,b,n ;
int i ;

printf ("enter number of term after ") ;
scanf ("%d",&n) ;
printf ("enter value x = ") ;
scanf ("%f",&x) ;

s = 0 ;

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

{
a = 1 + i ;
b = i ;

r = a * pow(x,b) ;

if ( i % 2 == 0)

s = s + r ;

else

s = s - r ;
//first tirm -2x (s = 0 - 2x)
}



s = 1 + s ;

printf ("sum = %10.2f \n",s) ;

}