In this example, you will learn how to multiply two floating-point numbers by taking input from the user and display the results on the screen.
Contents
You can learn more C Programming concepts here.
C Program to Multiply two floating-point numbers
#include <stdio.h>
int main() {
double numberOne, numberTwo, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &numberOne, &numberTwo);
// Calculating product
product = numberOne * numberTwo;
// %.3lf displays number up to 3 decimal point
printf("Product = %.3lf", product);
return 0;
}
Run Code on Online C CompilerOutput
Enter two numbers: 5.5 1.3
Product = 7.150
Program Explanation
In this example, the user is asked to enter two floating-point numbers. The two numbers entered by the user will be stored in numberOne and numberTwo respectively.
printf("Enter two numbers: ");
scanf("%lf %lf", &numberOne, &numberTwo);
Then the numberOne and numberTwo are multiplied using the * operator and the result is stored in product.
product = numberOne * numberTwo;
product variable is of type double, which is capable of storing value till 15 decimals. Here in this example, we can display the output restricting to 3 decimals using %.3lf (.3 represents 3 decimals after the point).
printf("Product = %.3lf", product);