In this tutorial you will learn about the C Program to Multiply two Floating Point Numbers and its application with practical example.
In this tutorial, we will learn to create a program to multiply two floating point numbers using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- C Variables
- C Data Types
- C Input Output
- C Operators
- C Function
Program to Multiply two Floating Point Numbers
In this program we will multiply two floating point numbers entered by user. We would first declared and initialized the required variables. Next, we would prompt user to input two floating point numbers. Later in the program we will multiply the numbers entered by user. We will then display the result to user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> int main(){ float n1, n2, product; printf("Enter first Number: "); scanf("%f", &n1); printf("Enter second Number: "); scanf("%f", &n2); //Multiply n1 and n2 product = n1 * n2; // Displaying result up to 2 decimal places. printf("Product of entered numbers is:%.2f", product); return 0; } |
Output:-
In the above program, we have first declared and initialized a set variables required in the program.
- n1 = it holds the first float number
- n2 = it holds the second float number
- product = it holds the product of n1 and n2
In the next statement user will be prompted to enter the two floating point number values which will be assigned to variable ‘n1’ and ‘n2’ respectively. Next, we will perform multiplication of both float numbers numbers (n1 and n2) and assign result to product. Now, we will be displaying the multiplication result of two floating point numbers using printf statement.
Program to multiply two numbers using function
In this program we will multiply two floating point numbers entered by user. We would first declared and initialized the required variables. Next, we would prompt user to input two floating point numbers. Later in the program we will multiply using a user defined function and display the product of the float numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> float multiply(float a, float b){ return a*b; } int main() { float n1, n2, product; printf("Enter first Number: "); scanf("%f", &n1); printf("Enter second Number: "); scanf("%f", &n2); // Calling multiply function product = multiply(n1, n2); // Displaying result up to 2 decimal places. printf("Product of entered numbers is:%.2f", product); return 0; } |
Output:-
In the above program, We have defined a user defined function called multiply(), which means to accept two float numbers and return product of the numbers passed as parameters.
Next, we have declared and initialized a set variables required in the program.
- n1 = it holds the first float number
- n2 = it holds the second float number
- product = it holds the product of n1 and n2
In the next statement user will be prompted to enter the two floating point values which will be assigned to variable ‘n1’ and ‘n2’ respectively. Next, we have called multiply function and assigned its return value to ‘product’. Now, we will be displaying the product of two float numbers using printf statement.