Sum of geometric progression terms

Asked

Viewed 503 times

0

I need to do a show that fits a1, q , n (number of geometric terms) to calculate the terms and then calculate the sum of those terms.

I was able to calculate the terms, but I have no idea how to calculate the sum.

Follow what I’ve done so far:

#include <stdio.h>

int main(){
    double a1, s;
    int i, q, n;

    scanf ("%lf %d %d", &a1, &q, &n);

    for (i=0; i<=n-1; i++){
     printf("%lf\n", a1);
     a1=a1*q;
    }

  return 0;
}
  • 2

    Welcome, the calculation of the sum of the terms of pg is: Sn = (a1 (q n - 1)/(q-1) correct? then already this: Sn = ((a1*(pow(q,n) - 1))/(q-1));

3 answers

1


Since you need the terms and of the sum, it is not complicated and nothing other than getting the terms. I improved a few things:

#include <stdio.h>

int main() {
    int a1, q, n, soma = 0;
    scanf("%d %d %d", &a1, &q, &n);
    for (int i = 0; i < n; i++) {
        printf("%d\n", a1);
        a1 *= q;
        soma += a1;
    }
    printf("%d\n", soma);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The formula solution may give a little more performance, but only testing to be sure.

If you enter high values the sum can burst easy, then you could use the type long long for this variable, but it would not improve much. Could use a doble then, but it doesn’t make much sense since it doesn’t need decimal. Then the right thing would be to use a guy who’s prepared for this, but it would make a lot of trouble for an exercise.

1

Since the formula for calculating the sum of the terms of an arithmetic progression is as follows:: inserir a descrição da imagem aqui

Being:

Sn = Sum of terms;

a1 = First term;

q = Reason;

n = number of terms;

This coded formula for the C language will be:

((a1*(pow(q,n) - 1))/(q-1))

However, there is an error in your code, in this excerpt:

for (i=0; i<=n-1; i++){
 printf("%lf\n", a1);
 a1=a1*q;
}

You are assigning values to the first term, if the purpose of the code were only to present the terms would be correct, however in this way your sum of the terms will be incorrect, to correct such error use an auxiliary variable to show the terms, I used the variable termo, but you can rename it.

termo=a1;
for (i=0; i<=n-1; i++){
 printf("O termo da posição %d  = %.0f\n", i, termo);
 termo=termo*q;
    }

Your code with some adjustments will look like this:

#include <stdio.h>
#include <math.h>

int main(){
    double a1, sn, termo;
    int i, q, n;
    printf("Insira o 1º Terno: ");
    scanf ( "%lf", &a1);
    printf("Insira  a razão : ");
    scanf ( "%d", &q);
    printf("Insira a quantidade de termos: ");
    scanf ( "%d", &n);

    termo=a1;
    for (i=0; i<=n-1; i++){
     printf("O termo da posição %d  = %.0f\n", i, termo);
     termo=termo*q;
        }
   sn = ((a1*(pow(q,n) - 1))/(q-1));
    printf("Soma dos termos da PG é: %.0f", sn);
  return 0;
}

Note: I didn’t understand why you are using double for the terms, however I didn’t modify anything related to this, only the output %.0f to display the numbers without the decimal part.

If there is any question regarding formatting I advise the readings:

Format specification syntax: printf and wprintf functions

C Tutorial - printf, Format Specifiers, Format Conversions and Formatted Output

  • 1

    I used double pq my teacher asked for it to be possible to insert decimal numbers

  • @Sther understood!

0

To make no sum it is only necessary to use the sum formula of the terms of a pg

Fórmula

To make exponentiation calculations in C include the library: <math.h> Tip: Use the function pow()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.