Calculation of percentage in C

Asked

Viewed 41,528 times

-2

" Encode, Compile and run a C program that receives the the basic salary of an employee, calculate and show the salary to be received, knowing that this employee has a 5% bonus on the basic salary, and pays 7% tax on the basic 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;
}
  • 2

    Please in addition to posting the code make it clear on the question what problem you encountered and what you have tried to do.

  • Vlw, the mustache already helped me !!!

1 answer

4


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

Second you’re calculating the wrong percentage anyway. You’re finding the percentage to be applied, dividing by 100, I don’t know why, and you’re not applying to the total.

As both the bonus, as the discount should be given on top of the basic salary, it is easy to do the bill. The formula needs to take the total value that is your 100%, so let’s normalize it to 1 (hence the division by 100 you were using, you already do it before you put it in the code). Then we normalize to 1 the percentages, that is 5%, turns 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 visualize with the division:

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

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

Finally, you’re using too many variables. It’s not an error, but it’s unnecessary.

In a real code several things could be done differently. I’m not going to say that the type 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • For more people like you I vote yes (:

  • I gave an even bigger simplification on ideone. I preferred not to do this initially for you to understand the process, but it could be simpler.

  • 1

    Luan, remember I asked you (in the previous version of this question) if your doubt was of thematic or programming? You had answered that it was programming, but now it is confirmed that it is even mathematics. I am not commenting badly, just so you realize this and focus your study on what is in fact its greatest difficulty. : ) So I suggest to take advantage of the good answer you had and do alternative exercises (with other similar problems even).

  • 1

    @Luizvieira had in fact not attacked me to this detail, I went on intuition when I was doing, since the tax can not be on the basic salary, but who am I to say that the exercise is what is wrong... : D Improved.

  • Vw to all who answered, I will look for more to do not want to stand at a point haha.

  • In the next question do not limit yourself to paste code, detail its difficulty/problem, as it may be the same as another OS user. @Maniero o salvador o/ hehehe

Show 1 more comment

Browser other questions tagged

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