Open In App

Practice Questions for Recursion | Set 4

Last Updated : 21 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Question 1 
Predict the output of the following program. 

C++




#include <iostream>
using namespace std;
 
void fun(int x)
{
    if(x > 0)
    {
        fun(--x);
        cout << x <<" ";
        fun(--x);
    }
}
 
int main()
{
    int a = 4;
    fun(a);
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10


C




#include<stdio.h>
void fun(int x)
{
  if(x > 0)
  {
     fun(--x);
     printf("%d\t", x);
     fun(--x);
  }
}
 
int main()
{
  int a = 4;
  fun(a);
  getchar();
  return 0;
}


Java




import java.io.*;
 
class GFG {
 
    static void fun(int x)
    {
        if(x > 0)
        {
            fun(--x);
            System.out.print(x + " ");
            fun(--x);
        }
    }
     
    public static void main (String[] args)
    {
        int a = 4;
        fun(a);
    }
}
 
// This code is contributed by SHUBHAMSINGH10


Python3




def fun(x):
     
    if(x > 0):
        x -= 1
        fun(x)
        print(x , end=" ")
        x -= 1
        fun(x)
         
# Driver code
a = 4
fun(a)
 
# This code is contributed by SHUBHAMSINGH10


C#




using System;
 
class GFG{
 
    static void fun(int x)
    {
        if(x > 0)
        {
            fun(--x);
            Console.Write(x + " ");
            fun(--x);
        }
    
     
    static public void Main ()
    {
        int a = 4;
        fun(a); 
    }
}
// This code is contributed by SHUBHAMSINGH10


Javascript




<script>
 
function fun(x)
{
    if (x > 0)
    {
        x -= 1
        fun(x)
        document.write(x + " ");
        x -= 1
        fun(x)
    }   
}
 
// Driver code
let a = 4;
fun(a)
 
// This code is contributed by bobby
 
</script>


Output: 

0 1 2 0 3 0 1

 

Time complexity: O(2n)

Auxiliary Space: O(n), since n extra space has been taken.
 

 
                   fun(4);
                   /
                fun(3), print(3), fun(2)(prints 0 1)
               /
           fun(2), print(2), fun(1)(prints 0)
           /
       fun(1), print(1), fun(0)(does nothing)
       /
    fun(0), print(0), fun(-1) (does nothing)

Question 2 
Predict the output of the following program. What does the following fun() do in general? 

C++




#include <iostream>
using namespace std;
  
int fun(int a[],int n)
{
  int x;
  if(n == 1)
    return a[0];
  else
    x = fun(a, n - 1);
  if(x > a[n - 1])
    return x;
  else
    return a[n - 1];
}
 
int main()
{
  int arr[] = {12, 10, 30, 50, 100};
  cout << " " << fun(arr, 5) <<" ";
  getchar();
  return 0;
}
 
// This code is contributed by shubhamsingh10


C




#include<stdio.h>
int fun(int a[],int n)
{
  int x;
  if(n == 1)
    return a[0];
  else
    x = fun(a, n-1);
  if(x > a[n-1])
    return x;
  else
    return a[n-1];
}
 
int main()
{
  int arr[] = {12, 10, 30, 50, 100};
  printf(" %d ", fun(arr, 5));
  getchar();
  return 0;
}


Java




import java.io.*;
  
class GFG {
  
    static int fun(int a[],int n)
    {
        int x;
        if(n == 1)
            return a[0];
        else
            x = fun(a, n - 1);
        if(x > a[n - 1])
            return x;
        else
            return a[n - 1];
    }
     
    public static void main (String[] args)
    {
        int arr[] = {12, 10, 30, 50, 100};
        System.out.println(" "+fun(arr, 5)+" ");
    }
}
 
// This code is contributed by shubhamsingh10


Python3




def fun( a, n):
    if(n == 1):
        return a[0]
    else:
        x = fun(a, n - 1)
    if(x > a[n - 1]):
        return x
    else:
        return a[n - 1]
 
# Driver code
arr = [12, 10, 30, 50, 100]
print(fun(arr, 5))
 
# This code is contributed by shubhamsingh10


C#




using System;
 
public class GFG{
    static int fun(int[] a,int n)
    {
        int x;
        if(n == 1)
            return a[0];
        else
            x = fun(a, n - 1);
        if(x > a[n - 1])
            return x;
        else
            return a[n - 1];
    }
     
    static public void Main ()
    {
        int[] arr = {12, 10, 30, 50, 100};
        Console.Write(" "+fun(arr, 5)+" ");
    }
}
 
// This code is contributed by shubhamsingh10


Javascript




<script>
//Javascript Implementation
 
function fun(a, n)
{
  var x;
  if(n == 1)
    return a[0];
  else
    x = fun(a, n - 1);
  if(x > a[n - 1])
    return x;
  else
    return a[n - 1];
}
  
// Driver code
var arr = [12, 10, 30, 50, 100];
document.write(fun(arr, 5));
// This code is contributed by shubhamsingh10
</script>


Output: 

100

 

Time complexity: O(n)

Auxiliary Space: O(n)
 

fun() returns the maximum value in the input array a[] of size n.

Question 3 
Predict the output of the following program. What does the following fun() do in general?

C++




#include <iostream>
using namespace std;
 
int fun(int i)
{
  if (i % 2) return (i++);
  else return fun(fun(i - 1));
}
  
int main()
{
  cout << " " << fun(200) << " ";
  getchar();
  return 0;
}
 
// This code is contributed by Shubhamsingh10


C




int fun(int i)
{
  if ( i%2 ) return (i++);
  else return fun(fun( i - 1 ));
}
 
int main()
{
  printf(" %d ", fun(200));
  getchar();
  return 0;
}


Java




import java.io.*;
 
class GFG {
    static int fun(int i)
    {
        if (i % 2 == 1) return (i++);
        else return fun(fun(i - 1));
    }
     
    public static void main (String[] args) {
        System.out.println(" " + fun(200) + " ");
    }
}
 
// This code is contributed by Shubhamsingh10


Python3




def fun(i) :
 
    if (i % 2 == 1) :
        i += 1
        return (i - 1)
    else :
        return fun(fun(i - 1))
   
print(fun(200))
 
# This code is contributed by divyeshrabadiya07


C#




using System;
 
class GFG{
     
static int fun(int i)
{
    if (i % 2 == 1) return (i++);
    else return fun(fun(i - 1));
}
 
// Driver code   
static public void Main ()
{
    Console.WriteLine(fun(200));
}
}
 
// This code is contributed by Shubhamsingh10


Javascript




<script>
 
function fun(i)
{
    if (i % 2 == 1)
        return (i++);
    else
        return fun(fun(i - 1));
}
 
// Driver code
document.write(fun(200));
 
// This code is contributed by bobby
 
</script>


Output: 

199

 

If n is odd, then return n, else returns (n-1). Eg., for n = 12, you get 11 and for n = 11 you get 11. The statement “return i++;” returns the value of i only as it is a post-increment. 

Time complexity: O(n)

Auxiliary Space: O(1)
 

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



Previous Article
Next Article

Similar Reads

Practice Questions for Recursion | Set 1
Explain the functionality of the following functions. Question 1 C/C++ Code int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } C/C++ Code int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } Java Code static int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } C/C
5 min read
Practice Questions for Recursion | Set 2
Explain the functionality of the following functions. Question 1 C/C++ Code /* Assume that n is greater than or equal to 1 */ int fun1(int n) { if (n == 1) return 0; else return 1 + fun1(n / 2); } Java Code /* Assume that n is greater than or equal to 1 */ static int fun1(int n) { if (n == 1) return 0; else return 1 + fun1(n / 2); } C/C++ Code # As
3 min read
Practice Questions for Recursion | Set 5
Question 1Predict the output of the following program. What does the following fun() do in general? C/C++ Code #include &lt;iostream&gt; using namespace std; int fun(int a, int b) { if (b == 0) return 0; if (b % 2 == 0) return fun(a + a, b/2); return fun(a + a, b/2) + a; } int main() { cout &lt;&lt; fun(4, 3) ; return 0; } // This code is contribut
5 min read
Practice Questions for Recursion | Set 6
Question 1 Consider the following recursive C function. Let len be the length of the string s and num be the number of characters printed on the screen. Give the relation between num and len where len is always greater than 0. C/C++ Code void abc(char *s) { if(s[0] == '&#92;&#48;') return; abc(s + 1); abc(s + 1); cout &lt;&lt; s[0]; } // This code
4 min read
Practice Questions for Recursion | Set 7
Question 1 Predict the output of the following program. What does the following fun() do in general? C/C++ Code #include &lt;iostream&gt; using namespace std; int fun(int n, int* fp) { int t, f; if (n &lt;= 2) { *fp = 1; return 1; } t = fun(n - 1, fp); f = t + *fp; *fp = t; return f; } int main() { int x = 15; cout &lt;&lt; fun(5, &amp;x) &lt;&lt;
4 min read
Practice Questions for Recursion | Set 3
Explain the functionality of below recursive functions. Question 1 C/C++ Code void fun1(int n) { int i = 0; if (n &gt; 1) fun1(n - 1); for (i = 0; i &lt; n; i++) cout &lt;&lt; &quot; * &quot;; } // This code is contributed by shubhamsingh10 C/C++ Code void fun1(int n) { int i = 0; if (n &gt; 1) fun1(n-1); for (i = 0; i &lt; n; i++) printf(&quot; *
3 min read
Practice questions for Linked List and Recursion
Assume the structure of a Linked List node is as follows. C/C++ Code struct Node { int data; struct Node *next; }; // This code is contributed by SHUBHAMSINGH10 C/C++ Code struct Node { int data; struct Node *next; }; Java Code static class Node { int data; Node next; }; // This code is contributed by shubhamsingh10 C/C++ Code class Node: def __ini
11 min read
Why is Tail Recursion optimization faster than normal Recursion?
What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recursive function in which the recursive call is the f
4 min read
Combination and Permutation Practice Questions | Set 1
Prerequisite : Permutation and Combination n students appear in an examination, find the number of ways the result of examination can be announced. Answer is 2n Examples: Input : n = 6 Output : Each student can either pass or fail in the examination. so ,there exists 2 possibilities for each of the 6 students in the result. hence total number of wa
3 min read
Practice Questions on Huffman Encoding
Huffman Encoding is an important topic from GATE point of view and different types of questions are asked from this topic. Before understanding this article, you should have basic idea about Huffman encoding. These are the types of questions asked in GATE based on Huffman Encoding. Type 1. Conceptual questions based on Huffman Encoding - Here are t
4 min read
Article Tags :
Practice Tags :