Skip to content
Permalink
c7c6ebeee6
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
46 lines (33 sloc) 945 Bytes
#include <stdio.h>
#include <math.h>
int main() {
float x;
int n;
double sum = 0.0;
/* Read a number from standard input. Execute the loop if it is successful. */
/* To exit, press Ctrl-D or Ctr-C */
while(scanf("%f %d", &x, &n) == 2)
{
for(int i=0; i<=n; i++)
{
if(i == 0){
sum = 0.0;
}
if(i == 1) {
sum += (double)x;
}
if(i>1){
if(i % 2 == 1){
sum += (((double)pow(x,i))/(double)i);
}
else {
sum -= (((double)pow(x,i))/(double)i);
}
}
}
printf("Sum: %.05lf\n", sum);
}
/* Let the OS know everything is just peachy. */
sum = 0;
return 0;
}