answersLogoWhite

0


Best Answer

When dividing one integer with another, there's a high probability that the answer will not be an integer. To deal with this, you must cast the numerator or denominator to a floating point type (float or double). By way of an example, consider the following code snippet:

int x=5, y=2;

int z = x/y;

float f1 = x/y;

float f2 = ( float ) x/y;

float f3 = x/( float ) y;

printf( "x=%d, y=%d, z=%d, f1=%f, f2=%f, f3=%f\n", x, y, z, f1, f2, f3 );

Output:

x=5, y=2, z=2, f1=2.000000, f2=2.500000, f3=2.500000

Clearly the correct answer is shown by f2 and f3. This is because we explicitly cast the numerator or denominator in each case. So long as we cast one or the other (or both), the result will be correct. If we don't cast either, then we're effectively only converting the result of the integer division, which is effectively the same as casting z to a float, as shown by f1.

See the related links section for in-depth information on floating point arithmetic in C/C++.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you make a division problem in c programming go into a decimal?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why do you make a remainder in division a fraction or decimal?

So that the answer from the division is a single quotient.


Why can you write a division problem as a fraction?

when you are writing a division problem as a fraction u take the remainder of your problem and make it the numerator and make the number you are dividing by the denominator


Can division make a number larger?

Only if you are dividing by a decimal or a fraction.


How do you make 4 over 24 a decimal?

Any fraction can be rearranged as a division problem. Divide the top number by the bottom number.In this case, divide 4 by 24 and get 0.166666... ■


How you make a ratio to a decimal?

Divide the numerator by the denominator using long division.


How do you make 4 over nine a decimal?

Divide 4.000.. by 9, as a long division, but make sure the decimal point for the answer is directly above the decimal point for 4.000... This should give you 0.444...


How do make a fraction to a decimal?

Do the division. 3/4 = 3 divided by 4 = 0.75


How do you you make 1 over 8 in to a decimal?

Do the long division. 1/8 = 0.125


How do you find a divisor?

make a division problem and find the lowest number


How do you make a fraction inyo a decimal?

You divide the numerator by the denominator. It may need to be a long division.


What direction does the decimal move in micrometers to millimeters?

When going from smaller to larger units, there will be fewer of them. That indicates division, which will make it appear as if the decimal point is going to the left.


How do you make a top heavy fraction into a decimal?

Division: Divide the numerator (top number) by the denominator (bottom number).