Anthony Bahlman Anthony Bahlman - 1 year ago 103
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 Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download