Percentage calculation in C

-2
  

"Encode, compile, and run a C program that receives the   base salary of an employee, calculate and show the salary to receive,   knowing that this employee has a 5% bonus on the   base salary, and pay 7% tax on base salary. "

#include <stdio.h>

int main()
{
    float salario,s1,s2,final;

    printf("Digite o salario: ");
    scanf("%f",&salario);

    s1=(salario*0.07)/100;
    s2=(salario*0.05)/100;
    final=s2-s1;


    printf("O salario com desconto: %f",final);

    return 0;
}
    
asked by anonymous 04.05.2016 / 05:19

1 answer

4

The first error is that you are applying the rebate before giving the bonus, which is the opposite of what the statement determines.

Second you're calculating wrong percentage even. You are finding the percentage to be applied, dividing by 100, I do not know why, and you are not applying to the total.

As both the bonus and the rebate must be given on top of the base salary, it is easy to make the account. The formula needs to pick up the total value that is its 100%, so let's normalize it to 1 (hence the division by 100 it was using, you already do it before putting it in the code). Then we normalize to 1 the percentages, that is 5%, turn 0.05 (divided by 100 again) and 7% turns 0.07. Then we add the total (1) with the bonus (0,05) and subtract the discount (0,07). Look at that simple formula.

If you want to view with the partition:

salario = salario * (100 / 100 + 5 / 100 - 7 /100)

Obviously we can simplify this. I've simplified the code below. In the test done in the ideone I already made the whole account.

Finally, you are using too many variables. It is not a mistake, but it is unnecessary.

In a real code several things could be done differently. I will not say that float is not suitable for monetary value .

#include <stdio.h>

int main() {
    float salario;
    printf("Digite o salario: ");
    scanf("%f", &salario);
    salario *= 1 + 0.05 - 0.07;
    printf("\nO salario com desconto: %f", salario);
    return 0;
}

See running on ideone .

    
04.05.2016 / 05:37