# Find N fractions that sum upto a given fraction N/D

Given a fraction **N/D**, the task is to split this fraction into N parts such that their sum is equal to the fraction N/D, i.e.,

**Note:** Represents the terms in terms of fractions, instead of floating point numbers.

Input:N = 4, D = 2

Output:4/5, 1/5, 1/3, 4/6

Explanation:

Therefore, it is a valid set of fractions such that their sum is

Input:N = 3, D = 4

Output:1/2, 1/10, 3/20

Explanation:

Therefore, it is a valid set of fractions such that their sum is

**Approach:** The key observation in the problem is that the first fraction numerator can be and then further denominators can be using the below recurrence relation.

Below is the implementation of the above approach:

## C++

`// C++ implementation to split the ` `// fraction into N parts ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to split the fraction ` `// into the N parts ` `void` `splitFraction(` `int` `n, ` `int` `d) ` `{ ` ` ` `int` `ar[n]; ` ` ` `int` `first = d + n - 1; ` ` ` `ar[0] = first; ` ` ` ` ` `// Loop to find the N - 1 ` ` ` `// fraction ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` `int` `temp = --first; ` ` ` `first++; ` ` ` ` ` `ar[i] = first * temp; ` ` ` `--first; ` ` ` `} ` ` ` ` ` `// Loop to print the Fractions ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(ar[i] % n == 0) ` ` ` `{ ` ` ` `cout << ` `"1/"` `<< ar[i] / n << ` `", "` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `cout << n << ` `"/"` `<< ar[i] << ` `", "` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` `int` `D = 2; ` ` ` ` ` `// Function Call ` ` ` `splitFraction(N, D); ` `} ` ` ` `// This code is contributed by Bhupendra_Singh ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to split the ` `// fraction into N parts ` ` ` `import` `java.util.Scanner; ` ` ` `class` `Solution { ` ` ` ` ` `// Function to split the fraction ` ` ` `// into the N parts ` ` ` `public` `static` `void` ` ` `splitFraction(` `int` `n, ` `int` `d) ` ` ` `{ ` ` ` ` ` `long` `ar[] = ` `new` `long` `[n]; ` ` ` `long` `first = d + n - ` `1` `; ` ` ` `ar[` `0` `] = first; ` ` ` ` ` `// Loop to find the N - 1 ` ` ` `// fraction ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) { ` ` ` `ar[i] = first * (--first); ` ` ` `} ` ` ` ` ` `// Loop to print the Fractions ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `if` `(ar[i] % n == ` `0` `) { ` ` ` `System.out.print( ` ` ` `"1/"` `+ ar[i] / n ` ` ` `+ ` `", "` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `System.out.print( ` ` ` `n + ` `"/"` `+ ar[i] ` ` ` `+ ` `", "` `); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main( ` ` ` `String[] args) ` `throws` `Exception ` ` ` `{ ` ` ` `int` `N = ` `4` `; ` ` ` `int` `D = ` `2` `; ` ` ` ` ` `// Function Call ` ` ` `splitFraction(N, D); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to split the ` `// fraction into N parts ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to split the fraction ` `// into the N parts ` `public` `static` `void` `splitFraction(` `int` `n, ` `int` `d) ` `{ ` ` ` `long` `[]ar = ` `new` `long` `[n]; ` ` ` `long` `first = d + n - 1; ` ` ` `ar[0] = first; ` ` ` ` ` `// Loop to find the N - 1 ` ` ` `// fraction ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` `ar[i] = first * (--first); ` ` ` `} ` ` ` ` ` `// Loop to print the Fractions ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(ar[i] % n == 0) ` ` ` `{ ` ` ` `Console.Write(` `"1/"` `+ ar[i] / n + ` `", "` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `Console.Write(n + ` `"/"` `+ ar[i] + ` `", "` `); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 4; ` ` ` `int` `D = 2; ` ` ` ` ` `// Function Call ` ` ` `splitFraction(N, D); ` `} ` `} ` ` ` `// This code is contributed by SoumikMondal ` |

*chevron_right*

*filter_none*

**Output:**

4/5, 1/5, 1/3, 4/6,

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Find Recurring Sequence in a Fraction
- Find the Nth digit in the proper fraction of two numbers
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ..... upto n terms
- Find all combinations that add upto given number
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Find sum of the series 1+22+333+4444+...... upto n terms
- Find minimum number of Log value needed to calculate Log upto N
- Find square root of number upto given precision using binary search
- Find initial sequence that produces a given Array by cyclic increments upto index P
- LCM and HCF of fractions
- Program to add two fractions
- Sum of given N fractions in reduced form
- Program to compare two fractions
- Product of given N fractions in reduced form
- HCF of array of fractions (or rational numbers)

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.