-3

I'm trying to show the decimal points after the number

#include <stdio.h>

int main() {

    int weight;
    int miles;
    int price;

    printf("Enter the packages weight \n");
    scanf("%d", &weight);

    printf("Enter how many miles to ship the package \n");
    scanf("%d", &miles);

    if(weight <= 15) {
        price = 15;
    }
    else {
        price = (15 + ((weight - 15) * .5 ));
    }
    if(miles % 500 == 0) {
        price += (miles / 500 * 10);
    }
    else {
        price += ((miles / 500 )* 10) + 10;
    }


    printf("It will cost $%d to ship an item weighing %d pounds %d miles \n", price, weight, miles);
    return 0;
}

for the price = (15+((weight-15)*.5)); When I plug in the numbers outside of the console it shows the decimal places. I'm probably missing the most simple thing...

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
SureShoy
  • 3
  • 2

1 Answers1

0

You should change the data type of the variable price to float (or double) if you wish to store some fractional part in it.

Also, since miles / 500 may create some decimal part (and miles itself can be floating-point!), it should also be made float or double.

Finally, in the printf arguments, do not forget to change the format specifier %d.

Prefer to use a floating-point data type for all the variables involved in some computation that might yield fractional numbers as it will be more comprehensible and if you wish to not see the decimal part in the output, then set the precision to 0 (%.0f).

skrtbhtngr
  • 2,223
  • 23
  • 29
  • He definitely should [not](http://ideone.com/6X8ope) do that. Unless you think it's sane to have 3.65 plus 0.05 not equal to 3.70! – David Schwartz Oct 28 '16 at 21:30
  • @DavidSchwartz: But we can take the difference of the expressions we want to compare and see if it is very small or not. – skrtbhtngr Oct 29 '16 at 12:44