0% found this document useful (0 votes)
42 views

Recursion Output

The function strange(int x, int y) recursively subtracts the smaller number from the larger number until x is less than or equal to y, then returns x. When called with values of 20 and 7, it will return 13. The function Recur(int n) recursively divides n by 2 and prints the results until n is less than or equal to 1. When called with a value of 10, it will recursively divide 10 by 2 and print the results. The function fun1(char s[], int x) recursively swaps the first and last characters of the string s and calls itself, incrementing x each time, until x is greater than the length of s divided by 2.

Uploaded by

bhaktikhanijo16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Recursion Output

The function strange(int x, int y) recursively subtracts the smaller number from the larger number until x is less than or equal to y, then returns x. When called with values of 20 and 7, it will return 13. The function Recur(int n) recursively divides n by 2 and prints the results until n is less than or equal to 1. When called with a value of 10, it will recursively divide 10 by 2 and print the results. The function fun1(char s[], int x) recursively swaps the first and last characters of the string s and calls itself, incrementing x each time, until x is greater than the length of s divided by 2.

Uploaded by

bhaktikhanijo16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Ques 1

static int strange(int x,int y)


{
if(x>=y)
{
x=x-y;
return strange(x,y);
}
else
return x;
}
System.out.println(strange(20,7));

Ques 2

void Recur(int n)
{
if (n>1)
{
System.out.print(n+ " ");
if (n%2 !=0)
{
n=3*n+1;
System.out.print(n + " ");
}
Recur (n/2);
}
}
Recur(10)
Ques3
The following functions are part of some class
i) What will be the output of fun1() when the value of s[] ={‘J’,’U’,’N’,’E’} AND X= 1?
ii) What will be the output of fun2() when the value of n =”SCROLL” ?
iii) State in one line what does the function fun1() do apart from recursion
void fun1(char s[], int x)
{
System.out.println(s);
char temp;
if(x<s.length/2)
{
temp=s[x];
s[x] = s[s.length-x-1];
s[s.length-x-1]=temp;
fun1(s, x+1);
}
}
void fun2(String n)
{
char c[]=new char[n.length()];
for(int i=0;i<c.length; i++)
c[i]=n.charAt(i);
fun1(c,0);
}
Ques4
The following is a part of some class. What will be the output of the function mymethod() when the
value of the counter is equal to 3? Show the dry run/working
void mymethod(int counter)
{
if(counter==0)
System.out.println(" ");
else
{
System.out.println("Hello" +counter);
mymethod(--counter);
System.out.println(" "+counter);
}
}
Ques5
The following function Mystery( ) is a part of some class. What will the function Mystery ( ) return
when the value of num=43629, x=3 and y=4 respectively? Show the dry run/ working.
int Mystery( int num, int x, int y)
{
if(num<10)
return num;
else
{
int z = num % 10;
if( z % 2 = = 0 )
return z*x + Mystery( num/10,x,y);
else
return z*y + Mystery( num/10,x,y);
}
}
Ques6
The following function magicfun( ) is a part of some class. What will the function magicfun( ) return,
when the value of n=7 and n=10, respectively? Show the dry run/working:
int magicfun( int n)
{
if ( n= = 0)
return 0;
else
return magicfun(n/2) * 10 + (n % 2);
}

Ques7
The following function is a part of some class. Assume ‘x’ and ‘y’ are positive
integers, greater than 0. Answer the given questions along with dry run / working.
void someFun(int x, int y)
{
if(x>1)
{
if(x%y==0)
{
System.out.print(y+“ ”);
someFun(x/y, y);
}
else
someFun(x, y+1);
}
}
(i) What will be returned bysomeFun(24,2)?
(ii) What will be returned bysomeFun(84,2)?
(iii) State in one line what does the function someFun( )do, apart from recursion?
Ques8
The following function Check( ) is a part of some class. What will the function Check( ) return when
the values of both ‘m’ and ‘n’ are equal to 5? Show the dry run / working
int Check (int m, int n)
{
if (n = = 1)
return − m − −;
else
return + + m + Check (m, −−n);
}
Ques9
int quiz( int n)
{
if ( n <= 1 )
return n;
else
return (--n % 2) + quiz(n/10);
}
(a) What will the function quiz( ) return when the value of n=36922?
(b) State in one line what does the function quiz( ) do, apart from recursion?
Ques 9
void numbers(int n)
{
if(n>0)
{
System.out.print(n+” “);
numbers(n-2);
System.out.print(n+” “);
}
}
String numbers1(int n)
{
if(n<=0)
return “”;
return ( (numbers1(n-1))+n+ ” “);
}
}
numbers(5)?
numbers1(6)?
Ques10

void perform (int p)


{
int x;
for(int i=1;i<=p;i++)
{
x=trial(i);
System.out.println(x+” “);
}
}
int trial(int n)
{
if(n==1)
return 2;
else if(n==2)
return 3;
else
return trial(n-2)+trial(n-1);
}
trial(4) ?
perform(5) ?

Ques 11

int check(int n)
{
if(n<=1)
return 1;
if(n%2==0)
return 1+check(n/2);
else
return 1+check(n/2+1);
}
I) n=25
II) n=10

Q1 factorial
Q2 prime
Q3 power
Q4 count no of digits
Q5 sum of digits
Q6 reverse a number
Q7 hcf
Q8 reverse a string
Q9 Fibonacci series
Q10 print factors
Q11 print prime factors

1. What happens when base case is not defined in a recursive method?

2. What is direct recursion?

3. Which data structure / principle is used to perform recursion?

4. advantages of recursion

You might also like