Anthony Bahlman Anthony Bahlman - 2 months ago 6
C Question

C multiplying and dividing

I am running a program for school and am running into a simple math problem that c is not calculating correctly I am simply trying to divide my output by a number and it is returning the original number.

int main(void)
{
float fcost = 599;
float shipt = (fcost / 120);
shipt = shipt * 120;

printf("%4.0f", shipt);
return 0;
}

Answer

From what was stated in the comments, you want the result of the division to be rounded down.

In order to do that, you should use int instead of float so that integer division is performed (which truncates the fractional part) instead of floating point division (which retains the fractional part):

int fcost = 599;
int shipt =  (fcost / 120);
shipt *=120;

printf("%d", shipt);
Comments