Search code examples
cline

I tried making a electricity price calculating c language program, but one line doesn't work


I tried making a electricity price calculating c language program, but one line doesn't work.

Here's my code.

#include <stdio.h>

int main()
{
    int usageElectric;      //amount of electricity used
    int basicMoney;         //basic price
    int totalMoney;         //total price
    double usageMoneyPerKw; //kw per used price
    double totalFinalMoney; //final usage price
    double tax;             //tax


    printf("put in the amount of electricity used (kw) : ");  //put in 150kw.
    scanf("%d", &usageElectric);

    basicMoney = 660;   //basic price = $660
    usageMoneyPerKw = 88.5; //kw per usage price : $88.5
    totalMoney = basicMoney + (usageElectric * usageMoneyPerKw);    

    tax = totalMoney * (9 / 100);   //This line is the problem line = doesn't work

    totalFinalMoney = totalMoney + tax; 

    printf("Tax is %d\n", tax);  // a line to show that the tax isn't being caluculated properly

    printf("The final usage price is %lf.", totalFinalMoney);

    return 0;
}

If the input is 150(kw), the totalFinalMoney should come out as $15189.150000

Can anyone help me out on why this line isn't working?

tax = totalMoney * (9 / 100);

If worked properly, it should come out as follows:

tax = 13935 * (9/100) = 1254.15

and therefore, the final outcome should be:

The final usage price is 15189.150000

Solution

  • In the subexpression 9/100 both operands are integers, so the division is integer division, meaning any fractional part is truncated, so it evaluates to 0.

    If you change to floating point constants, you'll get floating point division. So change the above to:

    9.0/100.0
    

    Or simply:

    0.09