🚀⚡Beginner
Multiply Two Floating-Point Numbers
Multiply two decimal numbers
This program multiplies two floating-point numbers (decimal numbers). It demonstrates working with the float data type and the %f format specifier.
📚 What You Will Learn
- •Using
floatdata type for decimal numbers - •Format specifier
%ffor floating-point - •Controlling decimal places with
%.2f
C Program to Multiply Two Floating-Point Numbers
multiply_floats.c
C
1#include <stdio.h>23int main() {4 // Declare floating-point variables5 float num1, num2, product;6 7 // Get first number8 printf("Enter first number: ");9 scanf("%f", &num1);10 11 // Get second number12 printf("Enter second number: ");13 scanf("%f", &num2);14 15 // Calculate product16 product = num1 * num2;17 18 // Display result with 2 decimal places19 printf("%.2f × %.2f = %.2f\n", num1, num2, product);20 21 return 0;22}Output
Enter first number: 3.5
Enter second number: 2.4
3.50 × 2.40 = 8.40
Detailed Code Explanation
Line 4
float num1, num2, product;float is used for decimal numbers with ~7 digits of precision.
| Type | Size | Precision | Range |
|---|---|---|---|
| float | 4 bytes | ~7 digits | ±3.4×10³⁸ |
| double | 8 bytes | ~15 digits | ±1.7×10³⁰⁸ |
Line 18
printf("%.2f", product);.2 controls decimal places. Without it, %f shows 6 decimal places by default.
Format Specifiers for Floating-Point
| Specifier | Example | Output |
|---|---|---|
| %f | printf("%f", 3.14159); | 3.141590 (default 6 decimals) |
| %.2f | printf("%.2f", 3.14159); | 3.14 (2 decimal places) |
| %.0f | printf("%.0f", 3.14159); | 3 (no decimals, rounded) |
| %10.2f | printf("%10.2f", 3.14); | 3.14 (width 10, right-aligned) |
| %e | printf("%e", 314159.0); | 3.141590e+05 (scientific notation) |
📝 Key Takeaways
✓
float for decimal numbers✓
%f format specifier for float✓
%.Nf controls N decimal places✓Use
double for more precisionRelated Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials