C Program To Multiply Two Floating Point Numbers

In this post, you are going to learn how to multiply two floating point numbers in C Programming language.

C Program To Multiply Two Floating Point Numbers

You will get to know two different methods to write this program. In the first example, the product of two floating point numbers entered by the user is calculated and printed on the screen.

While in the second example, we will create a function that multiplies the number, then we will call this user defined function in the main function of the program.

So, Without further ado, let’s begin the tutorial.

C Program To Multiply Two Floating Point Numbers

// C Program To Multiply Two Floating Point Numbers
#include <stdio.h>
int main(){
    double a, b, product;
    
    // Asking for the first Input
    printf("Enter First Number: ");
    scanf("%lf", &a);
    
    // Asking for the Second Input    
    printf("Enter Second Number: ");
    scanf("%lf", &b);
    
    // Calculating Product
    product = a * b; 
    
    // Displays Output
    printf("The Product of Two Numbers is %.2lf", product);
    return 0;
}

Output

Enter First Number: 5.7
Enter Second Number: 7.3
The Product of Two Numbers is 41.61

How Does This Program Work ?

double a, b, product;

In this example, we have declared three floating numbers a, b and product. Here, we have used double data type in this program because double data types are use to store high-precise floating numbers in C.

// Asking for the first Input
    printf("Enter First Number: ");
    scanf("%lf", &a);

// Asking for the Second Input    
    printf("Enter Second Number: ");
    scanf("%lf", &b);

The user is displayed a message asking to enter the numbers. The value of floating numbers is stored in the two variables a and b respectively.

// Calculating Product
    product = a * b; 

Then, the two floating numbers entered by the user are multiplied using * operator. And the result is stored in the product variable.

// Displays Output
    printf("The Product of Two Numbers is %.2lf", product);
    return 0;

Finally, the output is displayed on the screen using printf() function. Here we have used %.2lf because we wanted to show our result only till 2 decimal places.

Some Used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

scanf() – scanf() function is used to take input from the user. Here %lf indicates that we are reading floating type.

printf() – printf() function is used to display and print the string under the quotation to the screen.

// – Used for Commenting in C

C Program To Multiply Two Numbers Using Function

// C Program To Multiply Two Float Numbers Using Functions
#include <stdio.h>

float product(float x, float y){
    return x * y;
}

int main(){
    double a, b, result;
    
    // Asking for First Input
    printf("Enter First Number: ");
    // For Input
    scanf("%lf", &a);
    
    // Asking For Second Input
    printf("Enter Second Integer: ");
    scanf("%lf", &b);
    
    // Calling User Defined function
    result = product(a, b);
    
    // Displays Output 
    printf("The Product of Two Numbers is %.2lf", result);
    return 0;
}

Output

Enter First Number: 5.7
Enter Second Integer: 7.3
The Product of Two Numbers is 41.61
float product(float x, float y){
    return x * y;
}

Here in this example, we have generated a user defined function which provides us the product of any two floating point numbers.

Conclusion

I hope after going through this tutorial, you have clearly understood how to multiply two floating point numbers in C programming language.

If you still have any doubt regarding this, feel free to contact us in the comment section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *