Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm learning C language and working on tax and income homework, but I'm struggling as a beginner. Can someone help me out?

The question was related to the if-else topic. I tried to create a program where a person inputs their income and the program shows how much tax they need to pay.

#include<stdio.h>

int main ()
{
    float tax, income;

    printf("Kindly note down your income here\n");
    scanf("%f", income);

    if (income < 5.0 && income > 2.5)
    {
        printf("You have to pay 5 tax because your income is %f\n", income);
    }
    else if (income < 10.0 && income > 5.0)
    {
        printf("You have to pay 20 tax because your income is %f\n", income);
    }
    else if (income > 10.0)
    {        
        printf("You have to pay 30 tax because your income is %f\n", income);
    }
    else if (income < 2.5)
    {
        printf("You have to pay no tax because your income is %f\n", income);
    }

    return 0;
}

This is what I tried..Please figure out where I am wrong.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your compiler should warn you that you are not passing a variable of type float * corresponding to the format string "%f". Here is what gcc -Wall says:

1.c: In function ‘main’:
1.c:8:17: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
    8 |         scanf("%f", income);
      |                ~^   ~~~~~~
      |                 |   |
      |                 |   double
      |                 float *
`

and the fix is to change it to:

scanf("%f", &income);

Always check the return value of scanf() otherwise you may be operating on uninitialized variables.

When you test income you skip the values that are equal to the bound. You want to use a half interval, say, [2.5, 5) which you would write as income >= 2.5 && income < 5.

You probably want assign anything to the tax variable.

Finally, I suggest you eliminate the similar printf() statements.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading