error in percentage?

Asked

Viewed 54 times

2

A very interesting problem, the first two if me return valid values, but the latter returns a value different from the desired one. What to do?

#include <stdio.h>

int main (void)
{
    //declaracao de variaveis
    float salario;

    float final1; // 1 item
    float final2; // 2 item
    float final3; // 3 item

    //inicio
    printf("\nDigite o salario:R$ ");
    scanf("%f", &salario);

    if(salario<600.00)
        printf("\nIsento da taxa de contribuicao!");
    else
    if(salario > 600.00 || salario <=1200.00)
    {
        final1 = salario * 0.2;
        printf("\nA contribuicao sera de:R$ %2.f", final1);
    }

    else
    if(salario > 1200.00 || salario <= 2000.00)
    {
        final2 = salario * 0.25;
        printf("\nA contribuição sera de:R$ %2.f", final2);
    }

    else
    if(salario > 2000.00)
    {
        final3 = salario * 0.3;
        printf("\nA contribuição sera de:R$ %2.f", final3); 
    }


}

3 answers

1

Your if structure logic is incorrect, change all operators OR (||) for operator E(&&):

For when you enter, for example, 700 reais for the wage variable it satisfies the condition of the first if: salario > 600 and carries out the operation: final1 = salario * 0.2; so to avoid this modify to the operator E (&&)

Your if ties will look like this:

...
if(salario > 600.00 && salario <=1200.00)
....
if(salario > 600.00 && salario <=1200.00)
....

1

Turns out your condition if(salario > 600.00 || salario <=1200.00) is with a logical OU... With this, any number greater than 600 will fall in this condition, never reaching the other conditions.


You can fix this by putting in your conditions the logical operator E, in C represented by &&.

Thus correcting the condition in relation to 600:

if(salario >= 600.00 && salario <=1200.00)

And also the 1200:

if(salario > 1200.00 && salario <= 2000.00)

With this, your code will be more or less as follows:

#include <stdio.h>

int main (void)
{
    //declaracao de variaveis
    float salario;

    float final1; // 1 item
    float final2; // 2 item
    float final3; // 3 item

    //inicio
    printf("\nDigite o salario:R$ ");
    scanf("%f", &salario);

    if(salario<600.00)
        printf("\nIsento da taxa de contribuicao!");
    else
        if(salario >= 600.00 && salario <=1200.00)
        {
            final1 = salario * 0.2;
            printf("\nA contribuicao sera de:R$ %2.f", final1);
        }
        else
            if(salario > 1200.00 && salario <= 2000.00)
            {
                final2 = salario * 0.25;
                printf("\nA contribuição sera de:R$ %2.f", final2);
            }

            else
                if(salario > 2000.00)
                {
                    final3 = salario * 0.3;
                    printf("\nA contribuição sera de:R$ %2.f", final3); 
                }
}

See online: https://repl.it/repls/QuarrelsomeFreeSupercollider


There is also a point regarding your printing, you are omitting the decimal places with the mask %2.f... However I do not know if it was the desired, your percentage will always be displayed as an integer value.

1

The other answers explained the problem (use || instead of &&), but in fact you don’t need two conditions in if's. For example, in this if:

if (salario < 600.00) {
    printf("\nIsento da taxa de contribuicao!");
} else ...

If the salary is greater than or equal to 600, it does not enter the if and goes straight to else. So on arriving at the else, me I already know that at that point the value is >= 600 and testing this condition again is redundant. That is, it could just be:

if (salario < 600.00) {
    printf("\nIsento da taxa de contribuicao!");
} else if (salario <= 1200.00) { // aqui eu sei que é >= 600 e só preciso testar se é <= 1200
    ....
} else ...

The same thing goes for the second else. If you arrived there it is because the salary is not less than 600, nor less than or equal to 1200, IE, at that point it is certainly greater than 1200 and I do not need to test it again.

Using that same logic for the other conditions, it would look like this:

if (salario < 600.00) {
    printf("\nIsento da taxa de contribuicao!");
} else if (salario <= 1200.00) {
    printf("\nA contribuicao sera de:R$ %.2f", salario * 0.2);
} else if (salario <= 2000.00) {
    printf("\nA contribuição sera de:R$ %.2f", salario * 0.25);
} else {
    printf("\nA contribuição sera de:R$ %.2f", salario * 0.3); 
}

Notice that on the last else nor need of condition, because if it arrived there is because the salary is certainly greater than 2000 and does not need to test again.

Also note that if you just want to print the value and nothing else, you don’t even need the variables final1, final2 and final3. I changed the format to %.2f because the way it was before it did not show the two decimal places (the way I did seems to be what you want, since it comes to monetary values).

Although, if you’re working with monetary values,, don’t use float (for an exercise whatever, but for serious applications, no).

  • I understand. I’m sorry to ask, but what would be the recommended variable for monetary values?

  • @Joãorosa See the link above (that). The best is to use int and save the amount of pennies, the link explains better the reasons. Also has that one

  • @legal hkotsubo did not know this decimal type, so c# in comparison to C is best suited for monetary values? or, in c, using int with the pennies stored would perfectly realize the function of the decimal ?

  • 1

    @Luizaugusto In C think to use int is sufficient (or long depending on the values you will manipulate), as it already avoids the problems of rounding and other bizarrities of the float

Browser other questions tagged

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