How to calculate fourth root in C

Asked

Viewed 442 times

-2

Hello, I’m a beginner in the programming area and I’m doing a program that calculates some types of averages, but in the geometric average the formula uses root quarter of a.b.c.d, I thought to use the sqrt function but if I’m not mistaken it only makes square root, how can I do this account?

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

int main(void) {
  int cod;
  float nota1, nota2, nota3, nota4, media;

  printf("Insira a primeira nota: ");
    scanf("%f", &nota1);

  getchar();

  printf("Insira a segunda nota: ");
    scanf("%f", &nota2);

  getchar();  

  printf("Insira a terceira nota: ");
    scanf("%f", &nota3);

  getchar();  

  printf("Insira a quarta nota: ");
    scanf("%f", &nota4);

  getchar();    

  printf(" \n 1: Aritmetica \n 2: Harmonica \n 3: Geometrica \n 4: Quadratica \n\n Insira o codigo da media que deseja: ");
    scanf("%d", &cod);

  getchar(); 

  if (cod == 1){
    media = (nota1 + nota2 + nota3 + nota4 )/4;
      printf("Media: %.2f", media);
  }    
  if (cod == 2){
    media = (4/((1/nota1)+(1/nota2)+(1/nota3)+(1/nota4)));
      printf("Media: %.2f", media);    
  }
  if (cod == 3){
    media = 
      printf("Media: %.2f", media);    
  }
  if (cod == 4){
    media = (sqrt((pow(nota1, 2) + pow(nota2, 2) + pow(nota3, 2) + pow(nota4, 2))/4));
      printf("Media: %.2f", media);
  }
  return 0;
}
  • An alternative is to use the function pow(x, 1.0/4.0) of <Math. h>.

  • Thanks, I think it worked, I did so: if (Cod == 3){ media = Pow(Nota1 * nota2 * nota3 * nota4, 1.0/4.0); printf("Media: %.2f", media); }

  • The square root of the square root is the fourth root, so sqrt(sqrt(x)) would do. But as a general rule, use the above hint (root n of x is the same as x raised to 1/n)

1 answer

0

The function pow, include the math.h

int main (void)
{
  int a1, a2, a3, a4, soma;

  printf("Digite a1:");
  scanf("%d", &a1);

  printf("Digite a2:");
  scanf("%d", &a2);

  printf("Digite a3:");
  scanf("%d", &a3);

  printf("Digite a4:");
  scanf("%d", &a4);

  soma = a1 + a2+ a3 + a4;

  int r4 = pow (soma, 1.0/4); // aqui

  printf("soma = %d\n", soma);
  printf("O valor = %d\n", r4);

  system ("pause");

  return(0);
}

Browser other questions tagged

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