C Program to Multiply Two Floating-Point Numbers
C Program to Multiply Two Floating-Point Numbers
A school exercise asks you to multiply decimal values. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Multiplication computes a product; a double operand chooses floating-point arithmetic and printf precision controls its display.
Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.
Explain
Explain why multiply decimal values uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Order six notebooks at the same price.
Multiply Decimal Values: follow the data
Given data
A school exercise asks you to multiply decimal values. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Rule
Multiplication computes a product; a double operand chooses floating-point arithmetic and printf precision controls its display.
Observed result
Cost: 10.00
Understand the operation
A school order has a unit price and a count. Multiplying the two gives a total; the two-decimal format makes the shown fractional precision explicit.
Read the declarations and operands before the output call. %d formats an int; floating-point results use an appropriate precision with %f. A format describes a value that was already computed—it does not choose the earlier arithmetic.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
double price = 2.50;
int notebooks = 4;
printf("Cost: %.2f\n", price * notebooks);
return 0;
}
Expected output
Cost: 10.00
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
double price = 2.50;
int notebooks = 4;
printf("Cost: %.2f\n", price * notebooks);
return 0;
}
The unit price is 2.50 and the count is four.
The product is 10.0, displayed as 10.00.
Follow the worked calculation
Start with the sample
The unit price is 2.50 and the count is four.
Apply the rule
The product is 10.0, displayed as 10.00.
Check the result
Cost: 10.00
Cost: 10.00
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
double price = 2.50;
int notebooks = 4;
printf("Cost: %.2f\n", price * notebooks);
return 0;
}
Order six notebooks at the same price.
Worked answer
The worked answer follows the same rule with the requested changed data. Compare its complete source and output: Cost: 15.00. The result should follow from the operations, rather than from memorizing the first sample.
C17 · complete program
#include <stdio.h>
int main(void) {
double price = 2.50;
int notebooks = 6;
printf("Cost: %.2f\n", price * notebooks);
return 0;
}
Expected output
Cost: 15.00
Run in C compiler →Recognize the important boundaries
Binary floating point is approximate in general. A classroom price example is not a complete exact-currency accounting design.
The demonstrated values stay within the types’ representable ranges. A program accepting arbitrary external values needs checked conversion and bounds before performing arithmetic.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Multiplication computes a product; a double operand chooses floating-point arithmetic and printf precision controls its display.
Continue with arithmetic operand types.
Continue with validated input.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.