Multiply Floating‑Point Numbers Directly
#include <stdio.h>
int main() {
double num1, num2, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
product = num1 * num2;
printf("Product = %.2lf\n", product);
return 0;
}
Enter two numbers: 2.5 4.2
Product = 10.50
Multiplying two floating‑point numbers works the same as with integers: use the * operator to compute the product. The result is printed using a format specifier to control the number of decimal places.
Multiply Using a Function
#include <stdio.h>
double multiply(double a, double b) {
return a * b;
}
int main() {
double num1, num2;
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
printf("Product = %.2lf\n", multiply(num1, num2));
return 0;
}
Enter two numbers: 1.5 3.0
Product = 4.50
Encapsulating the multiplication logic in a function improves modularity. The multiply() function takes two numbers, computes the product, and returns it to the caller.
