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

Recursion Sheet

Uploaded by

jairatanmishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Recursion Sheet

Uploaded by

jairatanmishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

RECURSION SHEET

Question 1
The following class performs operations on strings using the concept of recursion. The
details of the class are given below:
Class name: theString
Data members / instance variables:
str to store the string.
Newstr to store new string.
Member functions
theString( ) constructor to assign null to all the strings.
void readStr( ) to input a string in str.
char changeCase (char d) to convert character stored in ‘d’ into its opposite
case and return.
void NewString (int len) using Recursive Technique and by involving
function changeCase( ), convert each letter of str into its opposite case and store new string in
Newstr.
void display( ) By invoking recursive function print the original string
and the new string.
Specify the class theString by giving the details of constructors and functions void readStr( ), char changeCase
(char), void NewString (int) and void display( ). You need to write the main( ) function also.

Question 2
A class Hifact has been defined to find the HCF of two numbers using the recursive technique. This HCF is used to
find the LCM of two numbers. Some members of the class are:
Class name: Hifact
Data members / instance variables:
a, b, hcf, lcm : integers
Member functions / Methods:
Hifact( ): constructor to assign initial values to the data members.
void getdata( ) to input values of a and b.
void change( ) to swap a and b if a > b.
int rechcf(int, int) to find hcf using the recursive technique and return.
int fn_lcm(int, int, int) to find and return lcm using a, b and hcf.
void result( ) to invoke rehcf( ) and fn_lcm( ) and to print lcm and hcf of two numbers a and b.
Specify the class Hifact giving details of the constructor and functions void getdata( ), void change( ), int rechcf(int,
int) and int fn_lcm(int, int, int). Write the main( ) function to find the hcf and lcm of two integers, a and b.

QUESTION 3
Class convert has been defined to express digits of an integer in words. The details of the class are givenBelow:
Class name: convert
Data members / instance variables:
n integer whose digits are to be expressed in words.
Member functions / methods:
convert( ) constructor to assign 0 to n.
void inpnum( ) to accept the value of n.
void extdigit(int) to extract the digits of n using the Recursive technique.
void num_to_words(int) to display the digits of an integer n in words.

Specify the class convert giving the details of the constructor and functions void inpnum(), void extdigit(int) and void
num_to_words(int). The main function need not be written. [10]

QUESTION 4:
A number is said to be a special number if the sum of the factorials of the digits is equal to the number. For
example:
145 5! = 5*4*3*2*1 = 120
4! =4*3*2*1 = 24
1! = 1 = 1
sum = 120 + 24 + 1 = 145

A class Find contains data members and member methods to check whether a number is a special number or not.
The details of the class are:
Class name: Find
Data members: n stores the integer
result :stores the boolean value.
Member functions:
Find(…) parameterized constructor to initialize the values of the data members.
int factorial(int x) calculates and returns the factorial of x using recursive technique.
void is_special( ) checks whether value in n is a special number or not and updates the value of data
member result.
void display() it displays whether n contains special number or not.
Write the code for class Find giving details of data members, constructor, int factorial(int), is_special() and display(
).Write the code for function main() to accept an integer and create the object of the class to check whether the
number entered is a special number or not. [10]
import java.util.*;
QUESTION 5:
A class Consonant defines function to accept a string and then from the string it extracts a substring which
contains only the consonants. For example if the input string is “The Book” then output is “ThBk”.
The details of the class are given below:
Class name : Consonant
Data member / instance variable:
str : stores the string.
Member functions / methods:
Consonant( ) : default constructor
void getstr( ) : accepts the string
String recConsonant(String S, int i): extracts and returns the string containing consonants only, using
recursive technique
void print( ) : prints the original string and the string which contains only the consonants by calling the above methods.
Specify the class Consonant giving details of the constructor and function void getstr( ) String
RecConsonant(String, int) and void print( ). Define the main( )
function to create an object and call the functions accordingly to enable the task. [10]
QUESTION 6
A class SeriesSum is designed to calculate the sum of the following series:

Some of the members of the class are:


Class name : SeriesSum
Data members / instance variables:
x: stores an integer
n: stores the number of terms
sum : stores the sum of the series.
Member functions / methods:
SeriesSum(int a, int b) : constructor to assign x = a and n = b
double findfact(int n) : calculates and returns the factorial of n using recursive technique
double findpower(int a, int b) : calculates and returns a raised to the power of b (i.e. ab) using recursive technique
void calculate( ) : calculates and prints the sum of the series by invoking the recursive functions
void display( ) : displays the sum of the series.
(a) Specify the class SeriesSum, giving details of the constructor (int, int),double findfact(int), double findpower(int,
int), void calculate( ) and void display( ). Define the main( ) function to create an object and call the functions
accordingly to enable the task. [8]
(b) State the two differences between iteration and recursion. [2]

QUESTION 7:
Design a class Array to accept 100 integers and sort them in ascending order and to
search an element. Some of the members of the class are given below:
Class name : Array
Data members / instance variables:
arr[ ] : array to store integers
size : stores the size of the array.
Member functions:
Array( ) : constructor to create the array and initialise size = 100
void accept( ) : accepts the 100 integer elements
void sort( ) : sorts the array in ascending order using bubble sort
technique
void binary(int n,Int l,int u) : searches the value n in the sorted array and prints
‘PRESENT’ if found otherwise prints ‘NOT
PRESENT’. Uses binary search technique
void display( ) : displays the sorted array in single line with one space
between the integers.
Specify the class Array giving details of the constructor( ), void accept( ), void sort( ),
void binary(int) and void display( ). Also define the main( ) function to create an
object and call the methods to enable the task.
Question 8 [2013]
Class Name : Emirp
Data Members/Instance Variables :
n : to store the number
rev : stores reverse of the number
f : stores the division
Member Functions/Methods :
Emirp(int nn) : to assign n=nn, rev=0, f=2
intisPrime (int x) : check if the no. is prime using the recursive technique and
returns 1 if prime otherwise returns 0
voidisEmirp() : Reverse the given no. and check if both the original no.
and reverse no. are prime by invoking the function
isPrime (int) and display the result with an appropriate
message.
Write the main function to create object of the class and call above member methods.
Question 9
A Happy Number is a number in which the eventual sum of the square of digits of the number is equal to 1. For ex
28=2 +82= 4 + 64= 68
2
[2012]
68 = 62 + 82=36 + 64 =100
100= 12 +02 + 02 = 1
Hence 28 is a happy no.
Design a class Happy to check if a given number is a magic number. Some of members of the class are given below:
Class Name : Happy
Data Members/Instance Variables :
n : stores the number
Member Functions :
Happy() : constructor to assign 0 to n
voidgetnum (intnn) : to assign the parameter value to the number n=nn
intsum_of_digits(int x) : returns the sum of squares of digits of the number x using
the recursive technique.
voidisHappy() : check if the given number is Happy by calling the function
sum_of_digits(int) and display appropriate message.
Write the main function to create object of the class and call above member methods.
Question 10
A class DeciHex as been defined to convert a decimal number into its equivalent hexadecimal no. some of the
members of the class are given below:
Class Name : DeciHex
Data Members/Instance Variables :
n : stores the decimal no.
Member Functions :
DeciHex() : constructor to initialize the data member n=0
void getnum (intnn) : to assign nn to n
String deci_hex( int ) : returns the hexadecimal equivalent of the integer using
recursive technique.
void show() : displays the decimal no. ‘n’. calls the function deci_hex()
and displays in hexadecimal equivalent.
Write the main function to create object of the class and call above member methods.
Question 11
A disarium number is a number in which the sum of the digits to the power of their respective position is equal to the
number itself. [2016]
1 2 3
Ex 135=1 + 3 + 5 =135
Hence 135 is a disarium number.
Design a class Disarium number to check if a given number is disarium number or not. Some of the
members of the class are given below:
Class Name : Disarium
Data Members/Instance Variables :
int num : stores the number
int size : stores the size of the number
Member Functions :
Disarium(int nn) : parameterized constructor to initialise the data members
n=nn and size=0
void countDigit() : count the total number of digits and assign it to size
int sumofDigits(int n,int p) : returns the sum of digits of the number n to power of their
respective positions p using recursive technique.
volid check() : checks whether the number is a disarium number and
display the result with an appropriate message.
Write main function to create object of the class and call the functions accordingly to enable the task.

Question 12
A class Palin has been defined to check whether a positive number is Palindrome number or not.
The number ‘N’ is a palindrome if the original number and its reverse are same. [2017]
Some of the members of the class are given below:
Class Name : Palin
Data Members/Instance Variables :
num : integer stores the number
revnum : integer to store the reverse of the number
Member Functions :
Palin() : constructor to initialize the data members with legal
initial values.
void accept() : to accept the number
int reverse(int y) : reverse the parameterized argument ‘y’ and stores in
‘revnum’ using recursive technique.
volid check() : checks whether the number is a Palindrome number by
invoking the function reverse() and display the result with
an appropriate message.
Write main function to create object of the class and call the functions accordingly to enable the task.
1) A class Palin has been defined to check whether a positive number is Palindrome number or not.
The number ‘N’ is a palindrome if the original number and its reverse are same. [2017]
Some of the members of the class are given below:
Class Name : Palin
Data Members/Instance Variables :
num : integer stores the number

Member Functions :
Palin() : constructor to initialize the data members with legal
initial values.
void accept() : to accept the number
int reverse(int y) : reverse the parameterized argument ‘y’ and Return in
using recursive technique.
void check() : checks whether the number is a Palindrome number by
invoking the function reverse() and display the result with
an appropriate message.
Write main function to create object of the class and call the functions accordingly to enable the task.
Question 13:
Design a class Perfect having following details: [2018]
Class Name : Perfect
Data Members/Instance Variables :
num : integer stores the number
Member Functions :
Perfect(in nn) : constructor to initialize the data member num=nn
int sum_of_factors(int i) : returns the sum of the factors of the number(num),
excluding itself, using recursive technique
void check() : checks whether the given number if perfect by invoking
the function sum_of_factrors() and display the result with
an appropriate message.
Write main function to create object of the class and call the functions accordingly to enable the task.
Question 14:
Design a class ArmNum to check if a given number is an Armstrong number of not. [A number is said to be
Armstrong if sum of its digits raised to the power of length of the number is equal to the number] [2019]
Ex: 371=33 + 73 + 13
1634=14 + 64 + 34 + 44
Some of the members of the class are given below:
Class name : ArmNum
Data Members/Instance Variables :
n : to store the number
l : to store the length of the number
Member Functions :
ArmNum(in nn) : parameterized constructor to initialize the data member n=nn
int sum_pow(int i) : returns the sum of each digit raised to the power of the length of
the number using recursive technique
void isArmstrong() : checks whether the given number is an Armstrong number by
invoking the function sum_pow() and display the result with an
appropriate message.
Write main function to create object of the class and call the functions accordingly to enable the task.
Question 15::
Design a class Perfect having following details: [2018]
Class Name : Perfect
Data Members/Instance Variables :
num : integer stores the number
Member Functions :
Perfect(in nn) : constructor to initialize the data member num=nn
int sum_of_factors(int i) : returns the sum of the factors of the number(num),
excluding itself, using recursive technique
void check() : checks whether the given number if perfect by invoking
the function sum_of_factrors() and display the result with
an appropriate message.
Write main function to create object of the class and call the functions accordingly to enable the task.
QUESTION 16
Design a class ArmNum to check if a given number is an Armstrong number of not. [A number is said to be
Armstrong if sum of its digits raised to the power of length of the number is equal to the number] [2019]
3 3 3
Ex: 371=3 + 7 + 1
1634=14 + 64 + 34 + 44
Some of the members of the class are given below:
Class name : ArmNum
Data Members/Instance Variables :
n : to store the number
l : to store the length of the number
Member Functions :
ArmNum(in nn) : parameterized constructor to initialize the data member n=nn
int sum_pow(int i) : returns the sum of each digit raised to the power of the length of
the number using recursive technique
void isArmstrong() : checks whether the given number is an Armstrong number by
invoking the function sum_pow() and display the result with an
appropriate message.
Write main function to create object of the class and call the functions accordingly to enable the task.
QUESTION 17
Write the main function to create object of the class and call above member methods.
Design a class NumDude to check if a given number is a Dudeney number or not.
(A Dudeney number is a positive integer that is a perfect cube, such that the sum of its digits is equal to
the cube root of the number.)
Example: 5832 = (5 + 8 + 3 + 2)3 = (18)3 = 5832 Some of the members of the class are given below:

Class name : NumDude


Data member/instance/variable:
num : to store a positive integer number
Methods/Member functions:
NumDude() : default constructor to initialise the data member with legal initial value
void input() : to accept a positive integer number
int sumDigits(int x) : returns the sum of the digits of number ‘x’ using recursive technique.
void is Dude() : checks whether the given number is a Dudeney number by invoking
the function sumDigits() and displays the result with an appropriate message
Specify the class NumDude giving details of the constructor(), void input(), int sumDigits(int)
and void isDude(). Define a main() function to create an object and call the functions accordingly to
enable the task.
1. Define a class Binary is declared as follows:
Class name : Binary
Data Members/Instance Variable:
a[] : integer array of 100 elements
size : size of the array
l : location of lower bound
u : location of upper bound
Member functions:
Binary (int nn) : parameterize constructor to assign size=nn
void input() : to accept the array elements
void sort() : sort the elements of combined array in ascending order
using Bubble sort technique.
int bin_search( int val) : returns the location of the value ‘val’ to be searched in the
list using binary searching technique using recursive
technique. If element is not found then it returns -1
void display() : displays the array elements
Define the main method.

2. A class Admission contains the admission numbers of 100 students. Some the data members/member functions
are given below: [2015]
Class name : Admission
Data Members/Instance Variable:
Adno[] : integer array to store admission nubmers
Member functions:
Admission() : constructor to assign array elements
void fillArray() : to accept the array elements in ascending order
int binsearech(int l, int u, int v) : to search for a particular admission number(v) using binary searching and
recursive technique and returns 1 if found otherwise returns -1.
Define the main method.

Outputs of class XI
1. The following function check() is a part of some class. What will be the function check() return when the value of
i) n=25 ii) n=10. Show the dry run/working. [2020]
int check(int n)
{
if(n<=1)
retur 1;
if(n%2==0)
return 1 + check(n/2);
else return 1 + check(n/2 + 1);
}

2. The following function Mystery() is a part of some class. What will the fucntion Mystery() return the value of num=43629,
x=3 and y=4 repectively? Show the dry run/working. [2019]
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);
}
}
3.
The following function magicfun () is a part of some class. What will the function magicflun() return when value of ‘n’ =retrun
magicfun(n/2)*10+(n%2)7 and n=10 respectively ? Show the dry run/working.
[2017]
int magicfun(int n)
{
if( n==0)
retrun 0;
else
return magicfun(n/2)*10+(n%2);
}

4.The following function Check () is a part of some class. What will the function Check() return when values of both ‘m’ and ‘n’ are
equal to 5? Show the dry run/working. [2016]
int Check(int m, int n)
{
if( n==1)
return –m--;
else
return ++m + Check(m, --n);
}
5 .The following function is a part of some class. Assume that ‘x’ and ‘y’ are positive integers, greater than 0. Answer the given
questions along with dry run/working. [2015]
void sumeFun(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 by someFun(24,2)?
ii) What will be returned by someFun(84,2)?
iii) State in one line what does the function someFun() do, apart from recursion?

6.The following function is a part of some class. [2014]


void fun1(char s[], int x)
{
System.out.println(s);
int 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<n.length();i++)
c[i]=n.charAt(i);
fun1(c,0);
}

i) What will be output of fun1() when the value of s[]={‘J’,’U’,’N’,’E’} and x=1
ii) What will be output of fun2() when the value of n=”SCROLL”
iii) State in one line what does the function fun1() do apart from recursion?
7. The following functin Recur() is a part of some class. What does the output of the fucntion Recur() when the value of n is equal
to 10. Show dry run/working. [2013]
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);
}
}
8.The following function witty() is a part of some class. What will be the output of witty() when value of n=”SCIENCE”and p=5.
Show dry run/working. [2012]
void witty(String n, int p)
{
if(p<=0)
System.out.println(“”);
else
{
System.out.println(n.charAt(p)+”.”);
witty(n,p-1);
System.out.println(n.charAt(p-1));
}
}
9.The following function mymethod() is a part of some class. What will be the output of witty() when value of counter is 3? Show
dry run/working. [2011]
void mymethod(int counter)
{
if(counter==0)
System.out.println(“ ”);
else
{
System.out.println(“Hello”+counter);
Mymethod(--counter)
System.out.println(“ ”+counter);
}
}

9.The following function number(int ) and number1(int) are part of some class. Answer the questions given below show dry
run/working:
public void numbers(int n)
{
if(n>0)
{
System.out.print(n+” ”);
numbers(n-2);
System.out.print(n+” ”);
}
}
public String numbers1(int n)
{
if(n<=0)
return “”;
return (numbers1(n-1)+n+” ”);
}
a) What will be the output of the funcion numbers( int n) when n=5?
b) What will be the output of the function numbers1( int n) when n=6
c) State in one line what is the function numbers1(int) doing apart from recursion.

10. The following function trial() and perform() are part of some class. Answer the questions give below. Show dry
run/working:
int trial(int n)
{
if(n==1)
return 2;
else if(n==2)
return 3;
else
return trial(n-2)+ trial(n-1);
}
void perform(int p)
{
int x;
for(int i=1;i<=p;i++)
{
x=trial(i);
System.out.print(x+” ”);
}
}
a) What will the function trial() return when n=4?
b) What will be the output of the function perform() when the value of p=5?
c) In one line state what the function trial() is doing apart from recursion?
11.The following function show() and calling() are parts of some class. Assume that the parameter n is greater than 1. Show
dry run/working:
void calling()
{
int f=2;
show(n,f);
}
int show(int n, int f)
{
if(n==f)
return 1;
if(n%f==0 || n==1)
return 0;
else
return(show(n,f+1));
}
a) What will be the function show() returns when calling() is invoked with n=11?
b) What will be the function show() returns when calling() is invoked with n=27?
c) State in one line, what the function show() executes?

12.The following function is a part of some class. Answer the questions that followed the function:
void Task(int N)
{
if(N>0)
{
int d=N%2;
Task (N/2);
System.out.print (d);
}
}
a) What will be the output when function Task(9) is invoked?
b) What will be the output when function Task(7) is invoked?
c) In one line state what the function Task() is doing apart from recursion?

You might also like