HomeC++C++ Program to Multiply Two Floating Point Numbers

C++ Program to Multiply Two Floating Point Numbers

In this post, you’ll learn to Multiply Two Floating Point Numbers in C++.

This lesson will help you learn how to multiply two floating point numbers, one of the various Mathematical functions using the C++ language. Let’s look at the below source code.

How to Multiply Two Floating Point Numbers in C++?

RUN CODE SNIPPET

Source Code

#include<iostream>
using namespace std;	
int main() 
{
    double a=2.56;
    double b=3.09;
    double c;
    cout<<"Value of a :"<<a;	
    cout<<"\nValue of b :"<<b;	
    c = a * b;
    cout<<"\nThe Product of a and b is :"<< c;
    
    return 0;
}

Output

Value of a :2.56
Value of b :3.09
The Product of a and b is :7.9104

#include <iostream>

  • This line which is called the header file is used in every C++ codding. #include statement tells the compiler to use available files.
  • The <iostream> is the name of the file which stands for Input and Output statement. This file includes the output statement ‘cout’, ‘cin’, and various other statements.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.
  • This statement must always end with a semicolon ‘ ; ‘.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.
  • The return statement must always end with a semicolon ‘ ; ‘.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. First declare the variables (a, b, c) as double values and assign the values to be calculated  double a=2.56 double b=3.09; double c;.
  2. The statement double is used to hold decimal numbers or floating point numbers.
  3. Next display the output statements with the cout and the Insertion Operator ‘ << ‘.
  4. When using characters/sentences, write them within quotes ” “, "The Product of" followed by ‘ << ‘ and the integer to display them.
  5.  Now we can include the statement to perform the Mathematical function – Product of two Floating Point numbers. Using the multiplication operator and assign an integer to store the answer c = a * b;.
  6. Using the output statement cout and the integer c, display the answer.
  7. End the function with return 0; this returns the function to main( ).

Now that you have understood how the source code performs its function, try it yourself with different values to it understand better.

Note: The ‘ \n ‘ in the code is used to insert a new line, to understand how it works exclude it from the code and work with it.

Additional Source Code

You can also try the source code given below, the following code is structured as to receive the values from the user rather than include the values directly in the code.

RUN CODE SNIPPET
#include<iostream>
using namespace std;	
int main() 
{
    double a, b;
    double c;
    cin >> a;
    cout <<"Value of a :"<<a;	
    cin >>b;
    cout <<"\nValue of b :"<<b;	
    c = a * b;
    cout<<"\nThe Product of "<<a<<" and "<<b<<" is : " << c;

    return 0;
}

Input

2.8
2.5

Output

Value of a :2.8
Value of b :2.5
The Product of 2.8 and 2.5 is : 7
  • The structure is similar to the previous code, the extra function we include is the cin>>a; and cin>>b; , this statement is used to collect and store the values that the user enters the assigned integer.
  • We use <<a<<  <<b<<   <<c to display the stored values in the output.
  • The rest of the statements and functions are similar, to perform the Multiplication Operation.

Share:

Leave A Reply

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

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022