C Program to Multiply two floating numbers

In this C Programming example, we will implement the program to multiply two floating numbers and print the output on the console.

1. How to Multiply two floating numbers?

Multiplying two floating point numbers can be done using two approaches.

  • The first method is the standard method where we prompt the user to input two floating-point numbers and then store them in the variables (say number 1 and number 2) and then calculate its product and print the output on the console.
  • The second method is using function where we prompt the user to input two floating-point numbers and then store them in the variables (say number 1 and number 2) and then apply the concepts of call by value and then call the function to calculate its product and print the output on the console.
Example

Input:
first Number: 1.1
second Number: 1.1

Output: 
The Product is: 1.210

Helpful topics to understand this program better are-


2. C Program to multiply two floating numbers

Let’s discuss the execution(kind of pseudocode) for the program to multiply two floating numbers in C.

2.1. Standard Method

  1. Initially, the program will prompt the user to enter number1 and number2 and store the value in these variables.
  2. Here, in product = number1 * number2 the calculation will be done by multiplying the numbers and storing the result in the Product.
  3. Finally, the result will be printed on the console.

Let us now implement the above execution of the program to multiply two floating numbers in C.

#include <stdio.h>
int main(){
  float number1, number2, product;
  printf("Enter the first Number: ");
  scanf("%f", &number1);
  printf("Enter the second Number: ");
  scanf("%f", &number2);

  product = number1 * number2;

  printf("The Product of the numbers is :%.3f ", product);
  return 0;
}

Note: In the above program, %.3f is used to display floating point number up to three decimal places.

Output
Enter the first Number: 2.1
Enter the second Number: 2.1
The Product of the numbers is : 4.410

2.2. Using Function

  1. Initially, the program will prompt the user to enter the two numbers and store the values in variables say (number1 and number2).
  2. Then we call multiply(number1, number2) function.
  3. In float multiply(float x, float y), float x, float y will represent number1 and number 2. When the function gets executed, return x * y will multiply the numbers and return their value to the main function.
  4. Now, in product = multiply(number1, number2) the value returned by the function gets stored in product and then gets printed on the console.
#include <stdio.h>
float multiply(float x, float y){
    return x * y;
}
int main(){
  float number1, number2, product;
  printf("Enter the first Number: ");
  scanf("%f", &number1);
  printf("Enter the second Number: ");
  scanf("%f", &number2);

  product  = multiply(number1, number2);

  printf("The Product of the numbers is : %.3f", product);
  return 0;
}

Note: In the above program, %.3f is used to display floating point number up to three decimal places.

Output
Enter the first Number: 3.3
Enter the second Number: 3.3
The Product of the numbers is : 10.890

3. Conclusion

In this C Programming example, we have discussed how to multiply two floating numbers in C.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index