Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C Program to Multiply two Floating Point Numbers?
In C, multiplying two floating-point numbers is a fundamental arithmetic operation. Floating-point numbers can represent real numbers with decimal points, such as 4320.0, -3.33, or 0.01226. The term "floating point" refers to the fact that the decimal point can "float" to support a variable number of digits before and after it.
Syntax
float result = float_num1 * float_num2; double result = double_num1 * double_num2;
Floating Point Data Types
| Type | Size | Range | Precision |
|---|---|---|---|
| float | 4 bytes | ±1.18 x 10-38 to ±3.4 x 1038 | 6-7 digits |
| double | 8 bytes | ±2.23 x 10-308 to ±1.80 x 10308 | 15-16 digits |
| long double | 12-16 bytes | ±3.36 x 10-4932 to ±1.18 x 104932 | 18-21 digits |
Example: Multiplying Two Float Numbers
This example demonstrates multiplication of two floating-point numbers with proper formatting −
#include <stdio.h>
int main() {
float a, b, result;
a = 11.23;
b = 6.7;
result = a * b;
printf("First number: %.2f<br>", a);
printf("Second number: %.2f<br>", b);
printf("Product: %.3f<br>", result);
return 0;
}
First number: 11.23 Second number: 6.70 Product: 75.241
Example: Using Double for Higher Precision
For calculations requiring higher precision, use double data type −
#include <stdio.h>
int main() {
double num1, num2, product;
num1 = 123.456789;
num2 = 987.654321;
product = num1 * num2;
printf("First number: %.6f<br>", num1);
printf("Second number: %.6f<br>", num2);
printf("Product: %.6f<br>", product);
return 0;
}
First number: 123.456789 Second number: 987.654321 Product: 121932.632653
Key Points
- Use
floatfor single precision (4 bytes) anddoublefor double precision (8 bytes). - The
printfformat specifier%.nfcontrols decimal places displayed. - Floating-point arithmetic may have small precision errors due to binary representation.
Conclusion
Multiplying floating-point numbers in C is straightforward using the * operator. Choose float for basic precision or double for higher precision calculations based on your requirements.
Advertisements
