The Wayback Machine - https://web.archive.org/web/20240213201612/https://www.geeksforgeeks.org/practice-questions-for-recursion-set-3/
Open In App
Related Articles

Practice Questions for Recursion | Set 3

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Explain the functionality of below recursive functions. 

Question 1 

C++




void fun1(int n)
{
   int i = 0;  
   if (n > 1)
     fun1(n - 1);
   for (i = 0; i < n; i++)
     cout << " * ";
}
 
// This code is contributed by shubhamsingh10


C




void fun1(int n)
{
   int i = 0; 
   if (n > 1)
     fun1(n-1);
   for (i = 0; i < n; i++)
     printf(" * ");
}


Java




static void fun1(int n)
{
   int i = 0;  
   if (n > 1)
     fun1(n - 1);
   for (i = 0; i < n; i++)
     System.out.print(" * ");
}
  
// This code is contributed by shubhamsingh10


Python3




def  fun1(n):
    i = 0
    if (n > 1):
        fun1(n - 1)
    for i in range(n):
        print(" * ",end="")
 
# This code is contributed by shubhamsingh10


C#




static void fun1(int n)
{
    int i = 0;
    if (n > 1)
        fun1(n-1);
    for (i = 0; i < n; i++)
        Console.Write(" * ");
}
 
// This code is contributed by shubhamsingh10


Javascript




<script>
 
function fun1(n)
{
    let i = 0;  
     
    if (n > 1)
        fun1(n - 1);
     
    for(i = 0; i < n; i++)
        document.write(" * ");
}
 
// This code is contributed by gottumukkalabobby
 
</script>


Answer: Total numbers of stars printed is equal to 1 + 2 + …. (n-2) + (n-1) + n, which is n(n+1)/2. 

Time complexity: O(n2)
Auxiliary Space:  O(n), due to recursion call stack

Question 2

C++




#define LIMIT 1000
void fun2(int n)
{
  if (n <= 0)
     return;
  if (n > LIMIT)
    return;
  cout << n <<" ";
  fun2(2*n);
  cout << n <<" ";
}
 
// This code is contributed by shubhamsingh10


C




#define LIMIT 1000
void fun2(int n)
{
  if (n <= 0)
     return;
  if (n > LIMIT)
    return;
  printf("%d ", n);
  fun2(2*n);
  printf("%d ", n);
}  


Java




int LIMIT = 1000;
void fun2(int n)
{
    if (n <= 0) return;
    if (n > LIMIT) return;
 
    System.out.print(String.format("%d ", n));
    fun2(2 * n);
    System.out.print(String.format("%d ", n));
}


Python3




LIMIT = 1000
def fun2(n):
    if (n <= 0):
        return
    if (n > LIMIT):
        return
    print(n, end=" ")
    fun2(2 * n)
    print(n, end=" ")
 
# This code is contributed by shubhamsingh10


C#




int LIMIT = 1000
void fun2(int n)
{
    if (n <= 0)
        return;
    if (n > LIMIT)
        return;
    Console.Write(n+" ");
    fun2(2*n);
    Console.Write(n+" ");
}
 
// This code is contributed by Shubhamsingh10


Javascript




<script>
 
let LIMIT = 1000;
function fun2(n)
{
    if (n <= 0)
        return;
    if (n > LIMIT)
        return;
 
    document.write(n + " "));
    fun2(2 * n);
    document.write(n + " "));
}
 
// This code is contributed by gottumukkalabobby
 
</script>


Answer: For a positive n, fun2(n) prints the values of n, 2n, 4n, 8n … while the value is smaller than LIMIT. After printing values in increasing order, it prints same numbers again in reverse order. For example fun2(100) prints 100, 200, 400, 800, 800, 400, 200, 100. 
If n is negative, the function is returned immediately. 

Time complexity: O(n)
Auxiliary Space:  O(log2n), due to recursion call stack

Please write comments if you find any of the answers/codes incorrect, or you want to share more information about the topics discussed above.


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!


Last Updated : 22 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials