Next: Summary
Up: The Assignment statement
Previous: Example Program: Temperature Conversion
The following program converts an input value in pence to the
equivalent value in pounds and pence. Note how integer division has
been used to find the whole number of pounds in the value of pence by
dividing pence by 100. Also how the %
operator has been used
to find the remainder when pence is divided by 100 to produce the
number of pence left over.
// Convert a sum of money in pence into the equivalent // sum in pounds and pence. #include <iostream.h> void main() { int pence, pounds; cout << "Enter the amount in pence: "; cin >> pence; cout << pence << " pence is "; pounds = pence / 100; // note use of integer division pence = pence % 100; // modulus operator -> remainder cout << pounds << " pounds and " << pence << " pence" << endl; }
Download program.