Find N fractions that sum upto a given fraction N / D | C ++ implementation to split the fraction into N parts ; Function to split the fraction into the N parts ; Loop to find the N - 1 fraction ; Loop to print the Fractions ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void splitFraction ( int n , int d ) { int ar [ n ] ; int first = d + n - 1 ; ar [ 0 ] = first ; for ( int i = 1 ; i < n ; i ++ ) { int temp = -- first ; first ++ ; ar [ i ] = first * temp ; -- first ; } for ( int i = 0 ; i < n ; i ++ ) { if ( ar [ i ] % n == 0 ) { cout << "1 / " << ar [ i ] / n << " , ▁ " ; } else { cout << n << " / " << ar [ i ] << " , ▁ " ; } } } int main ( ) { int N = 4 ; int D = 2 ; splitFraction ( N , D ) ; }