how do I use the for correctly?

Asked

Viewed 49 times

1

I’m doing an EP activity and he asked me for it in the image, but I got stuck in the sequence part, because, like the sequence is the top numbers is a sequence growing from 1 to infinity, and underneath are potentials of 2, i was wondering how can I make the 2 powers without the Math library, tried a test medium code, I also thought to put the value of the for in some incognita and use it to multiply the equation, but I do not know tbm how to store the value of for but nor scrolls, so to half lost.

inserir a descrição da imagem aqui

#include "stdio.h"
int main(void) {
  int t,i=0;
  float p,QuadNum;

  // seleção inicial 
  printf("digite um numero ");
  scanf("%d",&t);
  for( i=0;i<=t;i++){
QuadNum=i;
  }
  p= (1*2*3*4*5)/(QuadNum*i);
  printf(" o valor é  %f =",p);
  return 0;
}
  • To raise a number squared just multiply by itself. Ex: 3² = 33, 4² = 44

1 answer

1

Just multiply the denominator by 2.

See that there is a pattern:

  • the first term is 1 divided by 2
  • the second term is 2 divided by 4
  • the third term is 3 divided by 8
  • ...

That is, with each new term, the numerator increases by 1, and the denominator doubles in value.

So just do it:

float p = 1, denominador = 1;
int qtd;
printf("Digite a quantidade de termos:");
scanf("%d", &qtd);
for (int i = 1; i <= qtd; i++) {
    denominador *= 2;
    p *= i / denominador;
}
printf("O valor é: %f", p);

The numerator is controlled by for, 'cause if I have n terms, it varies from 1 to n (and that’s what the for makes, goes from 1 until the amount of terms informed).

The denominator starts at 1 and each iteration doubles (multiplied by 2).

Having the numerator and denominator, simply divide one by the other and multiply to the current value of p.

Browser other questions tagged

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