-->

program to get sum of 1+3/4+7/16+15/64..............................nth terms.

//program to get sum of 1+3/4+7/16+15/64..............................nth terms.
#include<stdio.h>
#include<conio.h>
void main()
{
   float i,n,k=2,sum=0,m=0;
   printf("enter  positive number for 'n'\n");
  scanf("%f",&n);
  for(i=1;i<=n;i++)
    {               
          sum=sum+(pow(k,i)-1)/(pow(k,m));
          m=m+2;    
     }
  printf("the sum=%f",sum);
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------------------
logics in mind:
->enter a number for range for which you want to get sum, say 'n'.
->let a variable 'sum' with initial value '0'
->we get sum using formula sum=sum+(pow(k,i)-1)/(pow(k,m)) because in terms 1+3/4+7/16+15/64...    ,
    ->we have two components namely numerator and denominator. Let's look at numerator;they are
           1,3,7,16.....it can be formulated as pow(k,i)-1 that is    21-1  .here the power goes on increasing
   ->similarly for denominator, it has power of 2 i.e.
                 20,22,24. we can see here that power is increasing by 2.
                for this we have,pow(k,m) then m=m+2
->we find sum inside loop
->at last we display the final sum.

No comments:

Post a Comment