Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use the ratio_multiply() function in C++

Harsh Jain

In this shot, we will learn how to use the ratio_multiply() function.

The ratio_multiply() function is available in the <ratio> header file in C++ and is used to multiply two ratios.

What is a ratio?

A ratio is a representation of a fraction in which the numerator and denominator are differentiated by the colon (:) symbol. The numerator and denominator can be of float data type.

Let’s understand with the help of an example.

  • Suppose that the first ratio is 10 : 3 and the second ratio is 5 : 2.

  • The result of the ratio_multiply() function for these two ratios is 25 : 3.

  • The ratios 10 : 3 and 5 : 2 can be represented as 103\frac{10}{3} and 52\frac{5}{2}​ in fractional form, respectively.

  • The multiplication = 103\frac{10}{3} * 52\frac{5}{2} = 506\frac{50}{6} = 253\frac{25}{3}


The ratio_multiply() method takes the following parameters:

  • Ratio1: A ratio object for multiplication.
  • Ratio2: Another ratio object to perform the multiplication with the first ratio object.

Return value

ratio_multiply() returns the result of multiplication in the simplest form. The function returns two constants:

  • num: The simplified numerator of the ratio after the multiplication of the two ratios.
  • den: The simplified denominator of the ratio after the multiplication of the two ratios.


Let’s have a look at the code.

#include <iostream>
#include <ratio>
using namespace std;

int main()
  typedef ratio<10, 3> ratio1;
  typedef ratio<5, 2> ratio2;

  typedef ratio_multiply< ratio1, ratio2 > sum;
  cout << "The ratio after multiplication is : ";
  cout << sum::num << "/" << sum::den;
  return 0;
Use ratio_multiply() function in C++


  • In lines 1 and 2, we import the required header files.

  • In line 5, we make a main() function.

  • In lines 7 and 8, we declare two ratios.

  • In line 10, we use the ratio_multiply() function to perform the multiplication between the two declared ratios.

  • In line 12, we display a message regarding the result.

  • In line 13, we display the numerator and denominator by accessing them through the multiplication variable, and display the result.

In this way, we can use the ratio_multiply() function to multiply two ratios and get the multiplication in the simplest form.



View all Courses

Keep Exploring