C++ Program To Multiply Two Floating-Point Numbers
Here, we will see how to multiply two floating-point numbers using the C++ program. Below are the examples:
Examples:
Input: A =1.2
B = 3.0Output: 3.6
Input: A = 4.5
B = 3.5Output: 15.75
There are 2 ways to multiply two floating-point numbers in C++:
- Using Multiplication Operator (*).
- Using Functions.
Let’s discuss these methods in detail.
1. Using Multiplication Operator (*)
In the below program to multiply two floating-point numbers A and B, the two floating-point numbers are multiplied using the arithmetic operator * and the product is stored in the variable product. Below is the C++ program to multiply two floating-point numbers:
C++
// C++ program to multiply // two floating point numbers #include <iostream> using namespace std; // Driver code int main() { // Input numbers double A = 1.2, B = 3.0; double product; // Multiplying two floating // point numbers product = A * B; // Printing the product cout << product; return 0; } |
3.6
Time Complexity: O(1)
Auxiliary Space: O(1)
2. Using Functions
In the below program to multiply two floating-point numbers A and B, the two floating-point numbers are multiplied using the function that uses arithmetic operator * and the product is stored in the variable product. Below is the C++ program to multiply two floating-point numbers:
C++
// C++ program to multiply two // floating point numbers #include <iostream> using namespace std; // Creating a user-defined function // called mul_floatnumbers that // multiplies the numbers passed to // it as an input. It gives you the // product of these numbers. float mul_floatnumbers( float a, float b) { return a * b; } // Driver code int main() { float A = 1.2, B = 3.0, product; // Calling mul_floatnumbers function product = mul_floatnumbers(A, B); // Printing the output cout << product; return 0; } |
3.6
Time Complexity: O(1)
Auxiliary Space: O(1)