Wednesday, August 17, 2011

QUES 20 ADD FIRST N TERMS OF THE GIVEN SERIES


C LANGUAGE BOOK



/*
WAP  PROGRAM TO ADD FIRST N TERMS OF THE GIVEN SERIES
    1 - 1/2! + 1/3! - 1/4! + 1/5!...      
*/


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

void main()
{

float val = -1,sum,var,value;
int x = -1,y = -1,i,n;

clrscr();

printf("\nENTER THE VALUE UPTO WHICH THE SERIES IS CACULATED\n");
scanf("%d",&n);

printf("\nTHE SERIES UPTO %d IS\n",n);
for(i = 1 ; i < n+1 ; i++)
{
y = y * -1;

if(i == n)
{
printf("1/%d...", fact(i));
break;
}

if(y == -1)
{
printf("1/%d + ", fact(i));
}

if(y == 1)
{
printf("1/%d - ", fact(i));
}
}

for(i = 1 ; i < n+1 ; i++)
{
value = fact(i);
x = x * -1;
sum = sum + x * (1/value);
}


printf("\n\nTHE SUM OF GIVEN SERIES IS: %f",sum);

return;
}

fact(int h)
{
int a;

if(h == 1)
 return h;
else
{
 a = h * fact(h-1);
 return a;
}

}















C LANGUAGE BOOK

No comments:

Post a Comment