Thursday, 15 August 2019

C programming for sum of GP series


/*Sum of GP series term by term*/


#include<stdio.h>
#include<math.h>
void
main()
{
            float a,r,s=0.0;
            int n,i;
            printf("enter the value of a,r,n;");
            scanf("%f %f %d",&a,&r,&n);
                        for(i=0; i<n;i++)
                        {
                                    s=s+a*pow(r,i);
                                    printf("\n the sum of the series after %d term is : %f \n",i,s);
                        }
                       
}



/*Sum of G.P series term by term*/
#include<stdio.h>
#include<math.h>
void
main ()
{
                float a,r,s=0.0;
                int n,i;
                printf("enter the value of a,r,n:");
                scanf("%f%f%d",&a,&r,&n);
                for(i=0;i<n;i++)
                {
                                s=s+a*pow(r,i);
               
                printf("\n the sum of the series after %d term is:%f\n",i,s);
}
}
Result:
enter the value of a,r,n:5 0.5 10
 the sum of the series after 0 term is:5.000000
 the sum of the series after 1 term is:7.500000
 the sum of the series after 2 term is:8.750000
 the sum of the series after 3 term is:9.375000
 the sum of the series after 4 term is:9.687500
 the sum of the series after 5 term is:9.843750
 the sum of the series after 6 term is:9.921875
 the sum of the series after 7 term is:9.960938
 the sum of the series after 8 term is:9.980469

 the sum of the series after 9 term is:9.990234
Press any key to continue

No comments:

Post a Comment

Thanks for comment.