C Program to Multiply two Floating Point Numbers
Program to Multiply Two Numbers
#include <stdio.h>
int main()
{
double firstNumber, secondNumber, product;
printf("Enter two numbers: ");
// Stores two floating point numbers in variable firstNumber and secondNumber respectively
scanf("%lf %lf", &firstNumber, &secondNumber);
// Performs multiplication and stores the result in variable productOfTwoNumbers
product = firstNumber * secondNumber;
// Result up to 2 decimal point is displayed using %.2lf
printf("Product = %.2lf", product);
return 0;
}
Output
Enter two numbers: 2.4 1.12 Product = 2.69
In this program, the user is asked to enter two numbers. These two numbers entered by the user are stored in variables firstNumber and secondNumber respectively. This is done using scanf()
function.
Then, the product of firstNumber and secondNumber is evaluated and the result is stored in the variable productOfTwoNumbers.
Finally, the productOfTwoNumbers is displayed on the screen using printf()
function.
Notice that, the result is round to the second decimal place using %.2lf
conversion character.
C program to multiply two floating-point numbers:
#include <stdio.h>
int main() {
float num1, num2, product;
printf("Enter the first number: ");
scanf("%f", &num1);
printf("Enter the second number: ");
scanf("%f", &num2);
product = num1 * num2;
printf("The product of %.2f and %.2f is: %.2f\n", num1, num2, product);
return 0;
}
In this program, we declare three variables: num1
, num2
, and product
.
We use the printf()
function to prompt the user to enter the two floating-point numbers. Then we use the scanf()
function to read the input values of num1
and num2
from the user.
We then calculate the product of the two floating-point numbers using the *
operator and store the result in the product
variable.
Finally, we use the printf()
function to display the result of the multiplication in the form of “The product of num1 and num2 is the product”. We use the %.2f
format specifier to display the floating-point numbers with two decimal places.
Note that the return 0;
statement at the end of the program indicates that the program has been executed successfully.