In this article, we will write a C program to multiply two floating-point numbers.
The program takes two floating point numbers from the user as input and prints their product on the screen as output.
Sample Input:
Enter the first number: 1.5
Enter the second number: 0.7
Output:
The product of the two numbers is: 1.050000
In C programming language, a floating-point number is represented by the float
and double
data types.
The float
data type is a single precision data type which uses 32 bits to store the number. Whereas, the double
data type uses 64 bits to store the floating point number.
The following program shows how you can multiply two floating-point numbers in C programming language:
// C program to multiply two floating-point numbers #include <stdio.h> int main(){ // Declare the variables float num1, num2, product; printf("Enter the first number: "); scanf("%f", &num1); printf("Enter the second number: "); scanf("%f", &num2); // Calculate the product product = num1 * num2; // Print the result printf("The product of the two numbers is: %f", product); return 0; }
Output:
Enter the first number: 1.5 Enter the second number: 0.7 The product of the two numbers is: 1.050000
Code Explanation:
- The program asks the user to enter two numbers which are stored in the
num1
andnum2
variables respectively. - The product of the two numbers is calculated by multiplying
num1
withnum2
and the result is stored in theproduct
variable. - Finally, the product of the two numbers is displayed on the screen using printf() function.
Example 2: Program to Multiply two Floating-point Numbers using Function
In this program, we will create a user-defined function multiply()
which takes two numbers as arguments and returns their product as a result.
The result returned by the multiply()
function is stored in the product
variable.
// C program to multiply two floating-point numbers #include <stdio.h> // User defined function to multiply two numbers float multiply(float num1, float num2){ float result; // Calculate the product result = num1 * num2; // Return the result return result; } int main(){ // Declare the variables float num1, num2, product; printf("Enter the first number: "); scanf("%f", &num1); printf("Enter the second number: "); scanf("%f", &num2); // Get the product product = multiply(num1, num2); // Print the result printf("The product of the two numbers is: %f", product); return 0; }
Output:
Enter the first number: 1.5 Enter the second number: 0.7 The product of the two numbers is: 1.050000
Thanks for reading.