Integers in C can only represent positive and negative integral numbers like -123, 10, 0, 88881823. When we divide an integer by another integer, the result is an integer with any leftover fraction truncated. So you 3 / 2 would return 1.

Sometimes we want the remaining part and there are several ways to deal with that part but here are two:
  1. Use floating point numbers instead of integers.
  2. Return the quotient and the remainder.
The C language provides floating point datatypes in single-precision and double-precision. Single-precision usually takes up 32 bits of space and double-precision usually takes up 64-bits of space. For more information on these two formats, please see the Wikipedia IEEE floating-point standard entry. If you don't care that much about the details, then you can just use floating point numbers as you would integers with a few things to keep in mind.

For floating point numbers, you need to declare them as float or double. When you go to print them out, use %f or %lf as the print mask. %lf is for long-float or double-precision.

The other approach is to return the quotient and the remainder.

Returning the quotient is easy as it is just a / b. One way to calculate the remainder is the expression a - ( a / b ) * b as the difference between the number and the truncated quotient multiplied by the divisor will be the remainder.

Another way is to use the modulus function %. You use it in this way: remainder = a % b. This means that the remainder is the integral amount left over after dividing a by b.

Write a program that prompts the user for two numbers and then finds the decimal part of the quotient and the integer remainder using the modulus function and prints them out.

Expected Output

Enter two numbers a and b: 14 5
Decimal part of quotient using floats: 0.800000
Integral Remainder using modulus: 4


Return to Homepage    Return to Programming Pages
Updated November 29, 2005. For comments and questions, send email to Vector.x64 @ (without the spaces).