Doubt in exercise with rule of 3

Asked

Viewed 67 times

1

I’m making a list of exercises and in one of them I need to calculate the inflation of two commodities from one month to the next. I was able to do the exercise for when inflation is <= 100%, but what if it’s bigger... any idea what I should do? Follows the code:

#include <stdio.h>

int main ()
{
  float valorMarco1, valorMarco2, valorAbril1, valorAbril2, infl1, infl2, x1, x2;

  printf("Insira o valor da mercadoria 1 em 01/março: ");
  scanf("%f", &valorMarco1);

  printf("Insira o valor da mercadoria 2 em 01/abril: ");
  scanf("%f", &valorAbril1);

  printf("Insira o valor da mercadoria 2 em 01/março: ");
  scanf("%f", &valorMarco2);

  printf("Insira o valor da mercadoria 2 em 01/abril: ");
  scanf("%f", &valorAbril2);

  x1 = (valorAbril1 * 100)/valorMarco1;
  infl1 = x1 - 100;

  x2 = (valorAbril2*100)/valorMarco2;
  infl2 = x2 - 100;

  printf("A inflação da mercadoria 1 teve o percentual de: %.1f \n", infl1);
  printf("A inflação da mercadoria 2 teve o percentual de: %.1f", infl2);
}

Sorry anything, I’m a beginner.

  • In case it seems to be a math problem, not C. As already answered, I closed here for organizing purposes, but you can mark your own answer so the post is as solved.

  • All right. I can only schedule my answer for six hours. If I could change the category of my question I would change, but I don’t know if I have more editing options.

1 answer

1


Guys, I was able to do it with the help of this site: https://math.stackexchange.com/questions/1070438/getting-percentage-difference-between-two-numbers

#include <stdio.h>

int main (){
float valorMarco1, valorMarco2, valorAbril1, valorAbril2, infl1, infl2;

    printf("Insira o valor da mercadoria 1 em 01/março: ");
    scanf("%f", &valorMarco1);

    printf("Insira o valor da mercadoria 2 em 01/abril: ");
    scanf("%f", &valorAbril1);


    printf("Insira o valor da mercadoria 2 em 01/março: ");
    scanf("%f", &valorMarco2);

    printf("Insira o valor da mercadoria 2 em 01/abril: ");
    scanf("%f", &valorAbril2);


    infl1 = ((valorAbril1 - valorMarco1)/valorMarco1)*100;
    infl2 = ((valorAbril2 - valorMarco2)/valorMarco2)*100;

    printf("A inflação da mercadoria 1 teve o percentual de: %.2f \n", infl1);
     printf("A inflação da mercadoria 2 teve o percentual de: %.2f \n", infl2);
}

But I did so first, so that I could be more step by step

div1 = valorAbril1/valorMarco1;
    mult1 = div1*100;
    infl1 = mult1 - 100;

Browser other questions tagged

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