🚀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 float data type for decimal numbers
  • Format specifier %f for floating-point
  • Controlling decimal places with %.2f

C Program to Multiply Two Floating-Point Numbers

multiply_floats.c
C
1#include <stdio.h>
2
3int main() {
4 // Declare floating-point variables
5 float num1, num2, product;
6
7 // Get first number
8 printf("Enter first number: ");
9 scanf("%f", &num1);
10
11 // Get second number
12 printf("Enter second number: ");
13 scanf("%f", &num2);
14
15 // Calculate product
16 product = num1 * num2;
17
18 // Display result with 2 decimal places
19 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 4float num1, num2, product;

float is used for decimal numbers with ~7 digits of precision.

TypeSizePrecisionRange
float4 bytes~7 digits±3.4×10³⁸
double8 bytes~15 digits±1.7×10³⁰⁸
Line 18printf("%.2f", product);

.2 controls decimal places. Without it, %f shows 6 decimal places by default.

Format Specifiers for Floating-Point

SpecifierExampleOutput
%fprintf("%f", 3.14159);3.141590 (default 6 decimals)
%.2fprintf("%.2f", 3.14159);3.14 (2 decimal places)
%.0fprintf("%.0f", 3.14159);3 (no decimals, rounded)
%10.2fprintf("%10.2f", 3.14); 3.14 (width 10, right-aligned)
%eprintf("%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 precision

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples