-1

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
Learner_15
  • 399
  • 2
  • 11
  • The `9/100` is calculated in integer math; force at lest one of them to be floating point, such as `(9.0/100.0)` so the whole computation is done in float. – Steve Friedl Dec 28 '19 at 16:44
  • It should be `tax = (totalMoney * 9) / 100.0;` – kiner_shah Dec 28 '19 at 16:57
  • 1
    You really shouldn't use floating-point numbers to represent monetary amounts. You should instead use an integer. – S.S. Anne Dec 28 '19 at 17:56
  • Does this answer your question? [Integer division always zero](https://stackoverflow.com/questions/9455271/integer-division-always-zero) –  Dec 29 '19 at 01:35

2 Answers2

0

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
dbush
  • 205,898
  • 23
  • 218
  • 273
-1

You simply have to typecast (9/10) like this ((double)9 / 100). Since now it is considering the output of 9/10 as integer and giving result as 0.

And while printing tax you should use %lf istead of %d.

#include <bits/stdc++.h>

using namespace std;

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 * ((double)9 / 100);   //This line is the problem line = doesn't work

    totalFinalMoney = totalMoney + tax; 

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

    printf("The final usage price is %lf.", totalFinalMoney);
}
Himanshu Singh
  • 2,117
  • 1
  • 5
  • 15