Find N fractions that sum upto a given fraction N / D | Java 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
import java . util . Scanner ; class Solution { public static void splitFraction ( int n , int d ) { long ar [ ] = new long [ n ] ; long first = d + n - 1 ; ar [ 0 ] = first ; for ( int i = 1 ; i < n ; i ++ ) { ar [ i ] = first * ( -- first ) ; } 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 ] + " , ▁ " ) ; } } } public static void main ( String [ ] args ) throws Exception { int N = 4 ; int D = 2 ; splitFraction ( N , D ) ; } }