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.
>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.