Doubt in percentage in C

Asked

Viewed 1,665 times

1

The user has to give a value that would be the price of the product and a value that would be the discount

When it comes to giving the ultimate value I can’t I’ve tried some things

#include <stdio.h>

int main ()
{
    float preco, desc, end;
    printf("Qual o valor da compra? ");
    scanf("%f", &preco);

    printf("Qual o valor do desconto? ");
    scanf("%f", &desc);

    end = preco / desc;
    printf("O valor final e: %f", end);
}

3 answers

1

Your formula for the discount calculation is wrong, in this case, as you want to discount the value you need to subtract the price by multiplying the price by the percentage value.
Then your code will look like this:

int main ()
{
    float preco, desc;
    printf("Qual o valor da compra? ");
    scanf("%f", &preco);

    printf("Qual o valor do desconto? ");
    scanf("%f", &desc);

    printf("O valor final e: %.2f", preco - (preco * desc/100));
}

That’s the way out:

Qual o valor da compra? 10
Qual o valor do desconto? 5
O valor final e: 9.50

Note that I removed the variable end Because you don’t need it. I hope I’ve helped :)

1

The correct calculation of the discounted value is:

end = preco - ( preco * desc / 100 );

whereas desc is the amount of the discount (in percent) between 0 and 100.

0


You can do it this way. Discounting 10% in the amount of R $ 30,00 we have: 30 * 0,90 = R$27,00 end = price * percentage;

for additions it is sufficient that the percentage value is this way "30 * 1.10"

Browser other questions tagged

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