
Understanding Recursion Through Simple and Clear Examples
1.Reverse a Number : Flow Chart : -In python : def reverse_num ( no , rev = 0 ): if no == 0 : return rev return reverse_num ( no // 10 , rev * 10 + no % 10 ) print ( reverse_no ( 1234 )) -In java : static int reverseNum ( int no , int rev ){ if ( no == 0 ){ return rev ; } return reverseNum ( no / 10 , rev * 10 + no % 10 ); } -In JavaScript : function reverseNum ( no , rev = 0 ){ if ( no === 0 ){ return rev ; } return reverseNum ( Math . floor ( no / 10 ), rev * 10 + no % 10 ); } 2.Count Digits : Flow Chart : -In Python : def count_digits ( no ): if no == 0 : return 0 return 1 + count_digits ( no // 10 ) print ( count_digits ( 2345 )) -In Java : static int countDigits ( int no ){ if ( no == 0 ){ return 0 ; } return 1 + countDigits ( no / 10 ); } -In JavaScript: function countDigits ( no ){ if ( no === 0 ){ return 0 ; } return 1 + countDigits ( Math . floor ( no / 10 )); } 3.Even or Odd : flow chart: -In Python : def is_even ( n ): if n == 0 : return True if n == 1 : return False return
Continue reading on Dev.to Python
Opens in a new tab


