Functions in Float Series

Started by thiruvasagamani, Sep 22, 2008, 11:34 AM

Previous topic - Next topic

thiruvasagamani

#include <stdio.h>
#include <conio.h>

float series( int n) // 'n' is number of terms to be added
{
float sum = 1; // store 1st term in sum since it as odd denominator
int i, sign = -1; //sign of 2nd term is -ve
for( i= 1; i<= n-1; i++) // execute loop n-1 times since 1st term is already stored in sum
{
sum + = sign * 1.0 / 2* i; // add each term in series to sum
sign = -sign; // alternative '-' and '+'
}
return( sum); // sum of 1st 'n' terms
}

main()
{
int n;
float series( int);
printf(" enter number of terms to be added");
scanf("%d", &n);
printf(" result is %f \n", series( n)); // display sum of 1st n terms in series
getch();
}


o/p:
If you enter 'n' value as 5 output is 1 - 1.0 /2 + 1.0/4 - 1.0 /6 + 1.0/8 is 0.7083333
Thiruvasakamani Karnan