Given the Numerator and Denominator of N fractions. The task is to find the product of N fraction and output the answer in reduced form.

**Examples:**

Input : N = 3 num[] = { 1, 2, 5 } den[] = { 2, 1, 6 } Output : 5/6 Product of 1/2 * 2/1 * 5/6 is 10/12. Reduced form of 10/12 is 5/6. Input : N = 4 num[] = { 1, 2, 5, 9 } den[] = { 2, 1, 6, 27 } Output : 5/18

The idea is to find the product of Numerator in a variable, say new_num. Now, find the product of Denominator in another variable, say new_den.

Now, to find the answer in Reduced form, find the Greatest Common Divisor of new_num and new_den and divide the new_num and new_den by the calculated GCD.

Below is the implementation of this approach:

## C++

`// CPP program to find product of N ` `// fractions in reduced form. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return gcd of a and b ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Print the Product of N fraction in Reduced Form. ` `void` `productReduce(` `int` `n, ` `int` `num[], ` `int` `den[]) ` `{ ` ` ` `int` `new_num = 1, new_den = 1; ` ` ` ` ` `// finding the product of all N ` ` ` `// numerators and denominators. ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `new_num *= num[i]; ` ` ` `new_den *= den[i]; ` ` ` `} ` ` ` ` ` `// Finding GCD of new numerator and ` ` ` `// denominator ` ` ` `int` `GCD = gcd(new_num, new_den); ` ` ` ` ` `// Converting into reduced form. ` ` ` `new_num /= GCD; ` ` ` `new_den /= GCD; ` ` ` ` ` `cout << new_num << ` `"/"` `<< new_den << endl; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `int` `num[] = { 1, 2, 5 }; ` ` ` `int` `den[] = { 2, 1, 6 }; ` ` ` ` ` `productReduce(n, num, den); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find product of N ` `// fractions in reduced form. ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` `// Function to return gcd of a and b ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Print the Product of N fraction in ` `// Reduced Form. ` `static` `void` `productReduce(` `int` `n, ` `int` `num[], ` ` ` `int` `den[]) ` `{ ` ` ` `int` `new_num = ` `1` `, new_den = ` `1` `; ` ` ` ` ` `// finding the product of all N ` ` ` `// numerators and denominators. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `new_num *= num[i]; ` ` ` `new_den *= den[i]; ` ` ` `} ` ` ` ` ` `// Finding GCD of new numerator and ` ` ` `// denominator ` ` ` `int` `GCD = gcd(new_num, new_den); ` ` ` ` ` `// Converting into reduced form. ` ` ` `new_num /= GCD; ` ` ` `new_den /= GCD; ` ` ` ` ` `System.out.println(new_num + ` `"/"` `+new_den); ` `} ` ` ` ` ` `// Driven Program ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `n = ` `3` `; ` ` ` `int` `num[] = { ` `1` `, ` `2` `, ` `5` `}; ` ` ` `int` `den[] = { ` `2` `, ` `1` `, ` `6` `}; ` ` ` ` ` `productReduce(n, num, den); ` ` ` ` ` `} ` `} ` ` ` `//This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find ` `# product of N fractions ` `# in reduced form. ` ` ` `# Function to return ` `# gcd of a and b ` `def` `gcd(a, b): ` ` ` `if` `(a ` `=` `=` `0` `): ` ` ` `return` `b; ` ` ` `return` `gcd(b ` `%` `a, a); ` ` ` `# Print the Product of N ` `# fraction in Reduced Form. ` `def` `productReduce(n, num, den): ` ` ` `new_num ` `=` `1` `; ` ` ` `new_den ` `=` `1` `; ` ` ` ` ` `# finding the product ` ` ` `# of all N numerators ` ` ` `# and denominators. ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `new_num ` `=` `new_num ` `*` `num[i]; ` ` ` `new_den ` `=` `new_den ` `*` `den[i]; ` ` ` ` ` `# Finding GCD of ` ` ` `# new numerator ` ` ` `# and denominator ` ` ` `GCD ` `=` `gcd(new_num, new_den); ` ` ` ` ` `# Converting into ` ` ` `# reduced form. ` ` ` `new_num ` `=` `new_num ` `/` `GCD; ` ` ` `new_den ` `=` `new_den ` `/` `GCD; ` ` ` ` ` `print` `(` `int` `(new_num), ` `"/"` `, ` ` ` `int` `(new_den)); ` ` ` `# Driver Code ` `n ` `=` `3` `; ` `num ` `=` `[` `1` `, ` `2` `, ` `5` `]; ` `den ` `=` `[` `2` `, ` `1` `, ` `6` `]; ` `productReduce(n, num, den); ` ` ` `# This code is contributed ` `# by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find product of N ` `// fractions in reduced form. ` ` ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return gcd of a and b ` ` ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` ` ` `} ` ` ` ` ` `// Print the Product of N fraction in ` ` ` `// Reduced Form. ` ` ` `static` `void` `productReduce(` `int` `n, ` `int` `[]num, ` ` ` `int` `[]den) ` ` ` `{ ` ` ` `int` `new_num = 1, new_den = 1; ` ` ` ` ` `// finding the product of all N ` ` ` `// numerators and denominators. ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `new_num *= num[i]; ` ` ` `new_den *= den[i]; ` ` ` `} ` ` ` ` ` `// Finding GCD of new numerator and ` ` ` `// denominator ` ` ` `int` `GCD = gcd(new_num, new_den); ` ` ` ` ` `// Converting into reduced form. ` ` ` `new_num /= GCD; ` ` ` `new_den /= GCD; ` ` ` ` ` `Console.WriteLine(new_num + ` `"/"` `+new_den); ` ` ` `} ` ` ` ` ` `// Driven Program ` ` ` `public` `static` `void` `Main () { ` ` ` ` ` `int` `n = 3; ` ` ` `int` `[]num = { 1, 2, 5 }; ` ` ` `int` `[]den = { 2, 1, 6 }; ` ` ` ` ` `productReduce(n, num, den); ` ` ` ` ` `} ` `} ` ` ` `//This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find product of N ` `// fractions in reduced form. ` ` ` `// Function to return ` `// gcd of a and b ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$a` `== 0) ` ` ` `return` `$b` `; ` ` ` `return` `gcd(` `$b` `% ` `$a` `, ` `$a` `); ` `} ` ` ` `// Print the Product of N ` `// fraction in Reduced Form. ` `function` `productReduce(` `$n` `, ` `$num` `, ` `$den` `) ` `{ ` ` ` `$new_num` `= 1; ` `$new_den` `= 1; ` ` ` ` ` `// finding the product of all N ` ` ` `// numerators and denominators. ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `$new_num` `*= ` `$num` `[` `$i` `]; ` ` ` `$new_den` `*= ` `$den` `[` `$i` `]; ` ` ` `} ` ` ` ` ` `// Finding GCD of new ` ` ` `// numerator and denominator ` ` ` `$GCD` `= gcd(` `$new_num` `, ` `$new_den` `); ` ` ` ` ` `// Converting into reduced form. ` ` ` `$new_num` `/= ` `$GCD` `; ` ` ` `$new_den` `/= ` `$GCD` `; ` ` ` ` ` `echo` `$new_num` `, ` `"/"` `, ` `$new_den` `,` `"\n"` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 3; ` ` ` `$num` `= ` `array` `(1, 2, 5); ` ` ` `$den` `= ` `array` `(2, 1, 6); ` ` ` ` ` `productReduce(` `$n` `, ` `$num` `, ` `$den` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

5/6

**How to avoid overflow?**

The above solution causes overflow for large numbers. We can avoid overflow by first finding prime factors of all numerators and denominators. Once we have found prime factors, we can cancel common prime factors.

**Note : **When you are asked to represent the answer in form of {P \times {Q} ^ {-1}} .

For these questions, first convert the numerator and denominator in reducible form P / Q as explained above. Then, find modular multiplicative inverse of Q with respect to a prime number m (Generally, 10^9 + 7) which is given in question. After finding modular multiplicative inverse of Q, multiply it with P and take modulo with given prime number m which gives us our required output.

// Thanks VaiBzZk for suggesting this condition.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of given N fractions in reduced form
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Maximum of sum and product of digits until number is reduced to a single digit
- Find N fractions that sum upto a given fraction N/D
- Count Pandigital Fractions pairs in given Array
- as_integer_ratio() in Python for reduced fraction of a given rational
- Find the index which is the last to be reduced to zero after performing a given operation
- Program to add two fractions
- Program to compare two fractions
- HCF of array of fractions (or rational numbers)
- LCM and HCF of fractions
- Print all proper fractions with denominators less than equal to N
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Largest number N which can be reduced to 0 in K steps
- Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
- Program for dot product and cross product of two vectors
- Reduce a given number to form a key by the given operations
- How to check if given four points form a square
- Check whether a very large number of the given form is a multiple of 3.
- Check if the given 2-D points form T-shape or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.