
Recursion
What is Recursion? Recursion means a function calls itself to solve a problem. Every recursive function has two main parts: Base Case → stops the function Recursive Call → calls the function again 1) 1 1 1 1 1 Flowchart Python def display ( num ): if num <= 5 : print ( 1 , end = " " ) display ( num + 1 ) display ( 1 ) Output: JavaScript function display ( num ) { if ( num <= 5 ) { console . log ( " 1 " ); display ( num + 1 ); } } display ( 1 ); Java public class Main { public static void main ( String [] args ) { display ( 1 ); } public static void display ( int num ) { if ( num <= 5 ) { System . out . print ( 1 + " " ); display ( num + 1 ); } } } 2. 1 2 3 4 5 Flowchart Python def display ( num ): if num <= 5 : print ( num , end = " " ) display ( num + 1 ) display ( 1 ) Output JavaScript function display ( num ) { if ( num <= 5 ) { console . log ( num + " " ); display ( num + 1 ); } } display ( 1 ); Java public class Main { public static void main ( String [] args ) { display ( 1 ); }
Continue reading on Dev.to JavaScript
Opens in a new tab



