Count number of ways to cover a distance | A naive recursive Java program to count number of ways to cover a distance with 1 , 2 and 3 steps ; Function returns count of ways to cover ' dist ' ; Base cases ; Recur for all previous 3 and add the results ; driver program
import java . io . * ; class GFG { static int printCountRec ( int dist ) { if ( dist < 0 ) return 0 ; if ( dist == 0 ) return 1 ; return printCountRec ( dist - 1 ) + printCountRec ( dist - 2 ) + printCountRec ( dist - 3 ) ; } public static void main ( String [ ] args ) { int dist = 4 ; System . out . println ( printCountRec ( dist ) ) ; } }