Sum of Series in C | Power Series in C | Geometric Progression

 Problem (Sum of Power Series)

The results of this series x0 + x1 + x2 + x3 + x4 + x5 + ……… + xk need to be found out.

power series

Input:

The program must first input an integer is 'T' ( where 1<=T<=100 ). Then take two integers X and K following T-th number of lines ( where x<=12 and k<=8 ).

Output:

The output of the program will print the results of the above series. Some inputs and outputs are given below.

Input Sample

Output Sample

-1

Invalid for the value of T

2

3 3

4 5

 

40

1365

3

2 10

5 10

10 5

 

Invalid for the value of X and K

Invalid for the value of X and K

111111

5

3 4

2 3

6 7

5 8

9 8

 

121

15

335923

488281

48427561

Solution for the Sum of Power Series using C

This is the sum of the two variables X and K. Here, K is the power of X. In the description of the problem, it is mentioned that an integer should be taken in the first line which is 'T'. Here we will take the variable T as an integer. X and K will take two integer inputs in the next T number line. Since we work with integers and say that the maximum value for X is 12 and K is 8 and also the T is 100, so here we have to take the data type integer. ( i and j variables are taken for the loop running )

int x, k, t, i, j; 

First, we can take input in T number of lines. So for this, we need to take the input for T using the scanf() function and use a for loop up to T to take input in T number of lines.

scanf("%d", &t);

for (i=1; i<=t; i++) {}

Now we have to take the input of X and K inside this loop. Here, we can take input using the scanf() function.

for (i=1; i<=t; i++) { scanf("%d %d", &x, &k); }

Now we have to do the main work. Since a task in a series tends to be repetitive, we must use a loop here. If we look at the series, we can see that the power of X has increased one by one. So here we have to run a for loop which will last till K. If we temporarily store its value in another variable in Xk-1 while running the loop, we can get the next term of the series just by multiplying the value of X with it each time. 

int FinalSum = 1, power = 1;
for (j=1; j<=k; j++)
{
      power = power * x;
      FinalSum += power;
}

For the print of the Final Sum output, we can use the printf() function.

 printf("%d\n", FinalSum);

Now, the full code will be like the below,

#include<stdio.h>
int main()
{
    int x, k, t, i, j;
    scanf("%d", &t);
    for (i=1; i<=t; i++)
    {
        scanf("%d %d", &x, &k);
        int FinalSum = 1, power = 1;
        for (j=1; j<=k; j++)
        {
            power = power * x;
            FinalSum += power;
        }
        printf("%d\n", FinalSum);
    }
    return 0;
}

We are missing some things. In the problem description, the value range of T, X, and K has been mentioned. So, the range of these variables should be written. We can do this by the if-else loop.

if (t>=1 && t<=100) {}
if (x<=12 && k<=8) {}

Here is the final code,

#include<stdio.h>
int main()
{
    int x, k, t, i, j;
    scanf("%d", &t);
    if (t>=1 && t<=100)
    {
        for (i=1; i<=t; i++)
        {
            scanf("%d %d", &x, &k);
            if (x<=12 && k<=8)
            {
                int FinalSum = 1, power = 1;
                for (j=1; j<=k; j++)
                {
                    power = power * x;
                    FinalSum += power;
                }
                printf("%d\n", FinalSum);
            }
            else
            {
                printf("Invalid for the value of X and K\n");
            }

        }
    }
    else
    {
        printf("Invalid for the value of T\n");
    }
    return 0;
}

Read Similar Post:

Post a Comment

Previous Post Next Post