Question 1 : Explain the functionality of the following functions.
using namespace std;
int fun1(int x, int y)
{
if (x == 0)
return y;
else
return fun1(x - 1, x + y);
}
int main()
{
cout << fun1(5, 2) << endl;
return 0;
}
int fun1(int x, int y)
{
if (x == 0)
return y;
else
return fun1(x - 1, x + y);
}
// Driver code
int main()
{
printf("%d\n", fun1(5, 2));
return 0;
}
static int fun1(int x, int y)
{
if (x == 0)
return y;
else
return fun1(x - 1, x + y);
}
// Driver code
public static void main(String[] args)
{
System.out.println(fun1(5, 2));
}
def fun1(x, y) :
if (x == 0) :
return y
else :
return fun1(x - 1, x + y)
# Driver code
print(fun1(5, 2))
using System;
class GFG {
static int fun1(int x, int y)
{
if (x == 0)
return y;
else
return fun1(x - 1, x + y);
}
// Driver code
static void Main(string[] args)
{
Console.WriteLine(fun1(5, 2));
}
}
function fun1(x, y)
{
if (x == 0)
return y
else
return fun1(x - 1, x + y)
}
// Driver code
console.log(fun1(5, 2));
Answer: This function adds the sum of first x natural numbers to y. It repeatedly decreases x and adds its value to y until x becomes 0. Final result: y + (1 + 2 + ... + x) = y + x(x + 1) / 2. For example, if x is 5 and y is 2, then fun should return 15 + 2 = 17.
Question 2 : Explain the functionality of the following functions.
using namespace std;
/* 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);
}
// Driver code
int main() {
cout << fun1(8);
return 0;
}
class GFG {
/* 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);
}
// Driver code
public static void main(String[] args)
{
System.out.println(fun1(8));
}
}
# Assume that n is greater than or equal to 1 */
def fun1(n):
if(n == 1):
return 0
else:
return 1 + fun1(n//2)
# Driver code
print(fun1(8))
using System;
// Assume that n is greater than or equal to 1
public class GFG {
static int fun1(int n)
{
if (n == 1)
return 0;
else
return 1 + fun1(n / 2);
}
// Driver code
static public void Main()
{
Console.WriteLine(fun1(8));
}
}
// Assume that n is greater than or equal to 1
function fun1(n)
{
if (n <= 1)
return 0
else
return 1 + fun1(Math.floor(n / 2))
}
// Driver code
console.log(fun1(8))
The above function calculates and returns ⌊log2(n)⌋. For example, if n is between 8 and 15 then fun1() returns 3. If n is between 16 to 31 then fun1() returns 4.
Question 3
using namespace std;
/* Assume that n is greater than or equal to 0 */
void fun2(int n)
{
if (n == 0)
return;
fun2(n / 2);
cout << n % 2;
}
// Driver code
int main()
{
fun2(21);
return 0;
}
#include <stdio.h>
void fun2(int n)
{
if(n == 0)
return;
fun2(n/2);
printf("%d", n%2);
}
// Driver code
int main() {
fun2(21);
return 0;
}
class GFG {
/* Assume that n is greater than or equal to 1 */
static void fun2(int n)
{
if (n == 0)
return;
fun2(n / 2);
System.out.print(n % 2);
}
// Driver code
public static void main(String[] args) { fun2(21); }
}
def fun2(n):
if(n == 0):
return
fun2(n // 2)
print(n % 2, end="")
# Driver code
fun2(21)
using System;
public class GFG {
static void fun2(int n)
{
if (n == 0)
return;
fun2(n / 2);
Console.Write(n % 2);
}
// Driver code
static public void Main()
{
fun2(21);
}
}
// Assume that n is greater than or equal to 1
function fun2(n)
{
if (n == 0)
return;
fun2(Math.floor(n / 2));
console.log(n % 2 + " ")
}
// Driver code
fun2(21)
Answer: The function fun2() prints the binary equivalent of n. For example, if n is 21 then fun2() prints 10101.
Question 4 : Predict the output of the following program.
#include <iostream>
using namespace std;
void fun(int x)
{
if(x > 0)
{
fun(--x);
cout << x <<" ";
fun(--x);
}
}
// Driver code
int main()
{
int a = 4;
fun(a);
return 0;
}
#include<stdio.h>
void fun(int x)
{
if(x > 0)
{
fun(--x);
printf("%d\t", x);
fun(--x);
}
}
// Driver code
int main()
{
int a = 4;
fun(a);
getchar();
return 0;
}
import java.io.*;
class GFG {
static void fun(int x)
{
if(x > 0)
{
fun(--x);
System.out.print(x + " ");
fun(--x);
}
}
// Driver code
public static void main (String[] args)
{
int a = 4;
fun(a);
}
}
def fun(x):
if(x > 0):
x -= 1
fun(x)
print(x , end=" ")
x -= 1
fun(x)
# Driver code
a = 4
fun(a)
using System;
class GFG{
static void fun(int x)
{
if(x > 0)
{
fun(--x);
Console.Write(x + " ");
fun(--x);
}
}
// Driver code
static public void Main ()
{
int a = 4;
fun(a);
}
}
function fun(x)
{
if (x > 0)
{
x -= 1
fun(x)
console.log(x + " ");
x -= 1
fun(x)
}
}
// Driver code
let a = 4;
fun(a)
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)
Explain the functionality of below recursive functions.
Question 5 : Predict the output of the following program.
using namespace std;
void fun1(int n)
{
int i = 0;
if (n > 1)
fun1(n - 1);
for (i = 0; i < n; i++)
cout << " * ";
}
// Driver code
int main()
{
fun1(4);
}
void fun1(int n)
{
int i = 0;
if (n > 1)
fun1(n-1);
for (i = 0; i < n; i++)
printf(" * ");
}
// Driver code
void main()
{
fun1(4);
}
class GFG {
static void fun1(int n)
{
int i = 0;
if (n > 1)
fun1(n - 1);
for (i = 0; i < n; i++)
System.out.print(" * ");
}
// Driver code
public static void main(String[] args) { fun1(4); }
}
def fun1(n):
i = 0
if (n > 1):
fun1(n - 1)
for i in range(n):
print(" * ", end="")
# Driver code
fun1(4)
using System;
class GFG {
static void fun1(int n)
{
int i = 0;
if (n > 1)
fun1(n - 1);
for (i = 0; i < n; i++)
Console.Write(" * ");
}
// Driver code
static void Main(string[] args) { fun1(4); }
}
function fun1(n)
{
let i = 0;
if (n > 1)
fun1(n - 1);
for (i = 0; i < n; i++)
console.log(" * ");
}
// Driver code
fun1(4)
Answer: Total numbers of stars printed is equal to 1 + 2 + .... (n-2) + (n-1) + n, which is n(n+1)/2.
Please write comments if you find any of the answers/codes incorrect, or you want to share more information about the topics discussed above.