12th Recursion function Assignments Year Wise_24708739
12th Recursion function Assignments Year Wise_24708739
Design a class BinSearch to search for a particular value in an array.Some of the members
of the
Member functions/methods:
void sort() : sorts the array elements in ascending order using any standard
sorting technique.
int binSearch(int l, int u, int v) : searches for the value „v‟ using binary search and recursive
returns -1.
Define the class BinSearch giving details of the constructor, void fillArray(), void sort() and
int binSearch(int, int, int). Define the main() function to create an object and call the
functions accordingly to enable the task.
JAVA MATLAB RAMJI SIR : 9305035029
Question 2019
[A number is said to be Armstrong if sum of its digits raised to the power of length of the
Example : 371 = 33 + 73 + 13
1634 = 14 + 64 + 34 + 44
54748 = 55 + 45 + 75 + 45 + 85
Thus 371, 1634 and 54748 are all examples of Armstrong numbers.
Methods/Member functions:
ArmNum (int 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
Specify the class ArmNum giving details of the constructor( ), int sum_pow(int) and
void isArmstrong( ). Define a main( ) function to create an object and call the
Question 2018.
Design a class Perfect to check if a given number is a perfect number or not. [ A number
is said to be perfect if sum of the factors of the number excluding itself is equal to the
original number]
Methods/Member functions:
num=nn
int sum_of_factors(int i) : returns the sum of the factors of the number(num), excluding
void check( ) : checks whether the given number is perfect by invoking the
function sum_of_factors( ) and displays the result with an appropriate message
Specify the class Perfect giving details of the constructor( ), int sum_of_factors(int) and
void check( ). Define a main( ) function to create an object and call the functions
JAVA MATLAB RAMJI SIR : 9305035029
accordingly to enable the task.
Question 2017
A class Palin has been defined to check whether a positive number is a Palindrome
number or not.
The number ‘N’ is palindrome if the original number and its reverse are same.
Methods/Member functions:
int reverse(int y) : reverses the parameterized argument ‘y’ and stores it in ‘revnum’
void check( ) : checks whether the number is a Palindrome by invoking the function
check( ). Define the main( ) function to create an object and call the functions accordingly to enable the
task.
JAVA MATLAB RAMJI SIR : 9305035029
Question 2016.
A disarium number is a number in which the sum of the digits to the power of their (10]
Example: 135 = 11 + 32 + 53
Design a class Disarium to check 1f a given number is a disarium number or not. Some
A class Admission contains the admission numbers of 100 students. Some of the data [10]
Member functions/methods:
int binSearch(int I, int u, int v) : to search for a particular admission number (v)
Specify the class Admission giving details of the constructor, void fillArray( ) and
int binSearch(int, int, int) . Define the main( ) function to create an object and call the
Question 2014.
X2 X4 X6 Xn
Sum= --- + --- + --- + ……….. ------
1! 3! 5! (n-1)!
Some of the members of the class are given below:
Class name : SeriesSum
Data members/instance variables:
x : to store an integer number
n : to store number of terms
sum : double variable to store the sum of the series
Member functions:
SeriesSum(int xx, int nn) : constructor to assign x=xx and n==nn
double findfact(int m) : to return the factorial of m using technique.
double findpower(int x, int y) : to return x raised to the power of y using recursive
technique.
void calculate( ) : to calculate the sum of the series by invoking the recursive
functions respectively.
void display( ) : to display the sum of the series.
(a) Specify the class SeriesSum, giving details of the constructor(int, int), [8]
JAVA MATLAB RAMJI SIR : 9305035029
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.
(b) State the two differences between iteration and recursion. [2]
Question 2013.
An emirp number is a number which is prime backwards and forwards. Example : 13 and
31 are both prime numbers. Thus, 13 is an emirp number.
Design a class Emirp to check if a given number is Emirp number or not. Some of the
Member functions:
int isprime(int x) : check if the number is prime using the recursive technique and return 1
void isEmirp( ) : reverse the given number and check if both the original number and
isEmirp(). Define the main( ) function to create an object and call the methods to check for
Emirp number.
Question 2012
A happy number is a number in which the eventual sum of the square of the digits of the
number is equal to 1.
Example: 28 = ( 2 )2 + ( 8 )2 = 4 + 64 = 68
68 = ( 6 )2 + ( 8 )2 = 36 + 64 = 100
100 = ( 1 )2 + ( 0 )2 + ( 0 )2 = 1 + 0 + 0 = 1
Hence, 28 is a happy number.
Example: 12 = ( 1 )2 + ( 2 )2 = 1 + 4 = 5
Hence, 12 is not a happy number.
Design a c1ass Happy to check if a given number is a happy number. Some of the members
of the class are given below:
class name : Happy / Ramji_Number
Member functions:
int sum_sq_digits(int x) : returns the sum of the square of the digits of the number x, using
Specify the class Happy giving details of the constructor( ), void getnum(int),
int sum_sq_digits(int) and void ishappy( ). Also define a main ( ) function to create an
object and call the methods to check for happy number.
Question 2011.
A class DeciOct has been defined to convert a decimal number into its equivalent octal
number. Some of the members of the class are given below:
Member functions:
void deci_oct() : calculate the octal equivalent of ‘n’ and store it in oct using
recursive technique.
void show() : display the decimal number ‘n’ calls the function deci_oct() and
(a) Specify the class DeciOct, giving details of the constructor( ), void getnum(int), [8]
void deci_oct() and void show(). Also define a main() function to create an object
JAVA MATLAB RAMJI SIR : 9305035029
and call the functions accordingly to enable the task.
Question 2010.
Design a class Change to perform string related operations. The details of the class are
given below:
Data members
Member functions:
char caseconvert(char ch) : converts the case of the character and returns it
void recchange(int) : extracts characters using recursive technique and changes its
case using caseconvert() and forms a new word.
a) Specify the class Change, giving details of the constructor(), member functions void
inputword(), char caseconvert(char ch), void recchange(int) and void display(). Define the
main function to create an object and call the functions accordingly to enable the above
change in the given word. [8]
JAVA MATLAB RAMJI SIR : 9305035029
b) Differentiate between finite and infinite recursion. [2]
Question 2009.
Class Binary contains an array of n integers (n<=100) that are already arranged in ascending
order. The subscripts of the array elements vary from 0 to n-1. The data members and
member functions of class Binary are given below:
Data members :
n : size of array
Member functions:
Binary(int nn) : constructor to initialize the size n to nn and the other instance
variables.
int binary_search(int v) : returns the location of the value(v) to be searched in the list by
a) Specify the class Binary giving details of the constructor, void readdata() and int
binary_search(int). you need not write the main function. [8]
b) State the base case in the recursive technique binary_search(). [1]
c) What are the drawbacks of using the recursive technique. [1]
JAVA MATLAB RAMJI SIR : 9305035029
Question 2008.
A class Revstr defines a recursive function to reverse a string and check whether it is a
palindrome. The details of the class are given below:
Data members
Member functions
void check() : to display the original string, its reverse and whether the string is
a palindrome or not.
Specify the class Revstr giving details of the functions void getStr(), void recReverse(int)
and void check(). The main function need not be written.
JAVA MATLAB RAMJI SIR : 9305035029
Question 2007.
Class Convert has been defined to express digits of an integer in words. The details of the
class are given below:
Data members
Member functions
Specify the class Convert giving details of the constructor and function void inpnum(), void
extdigit(int) and void num_to_words(int). The main function need not be written.
JAVA MATLAB RAMJI SIR : 9305035029
Question 12.
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 given below:-
Data members
Member functions
void result() : to invoke rechcf() and fn_lcm() and to print lcm, hcf of the two
numbers a and b.
Specify the class Hifact giving details of constructor, void getdata(), void change(), int
rechcf() and int fn_lcm(). Write the main function and find the hcf and lcm of any two
integers a and b. [10]
JAVA MATLAB RAMJI SIR : 9305035029
Question 2005.
A class recursion has been defined to find the Fibonacci series upto a limit. Some of the
members of the class are given below:
Data Members
limit : int
Member functions
int fib(int n) : to return the nth Fibonacci term using recursive technique.
void genearatefibseries() : to generate the Fibonacci series upto the given limit.
a) Specify the class recursion giving details of the constructor, int fib() , void
generatefibseries(). You may assume other functions are written for you and you need not
b) Why recursive functions result into slower execution of the program? [2]
JAVA MATLAB RAMJI SIR : 9305035029
PRACTICE NEW QUESTIONS
Question SP2016 [10]
A Special number is a number in which the sum of the factorial of its digits is equal to
the number. Example: 145 ( 1! + 4! + 5! = 145 ). Thus, 145 is a special number.
Design a class Special to check if the given number is a Special number or not. Some
of the members of the class are given below:
Member functions :
boolean isSpecial( ) : checks for the special number by invoking the function
Specify the class Special, giving details of the Constructor, void read( ), int
factorial(int), boolean isSpecial( ) and void display( ). Define the main() function to
create an object and call the member function according to enable the task.
JAVA MATLAB RAMJI SIR : 9305035029
Question 8
The Combination function C(n , k) gives the number of different (unordered ) K – elements Subsets that
can be found in a given set of n elements.
n : integer number
k : integer number
Member functions :
(a) Specify the class Combination, giving details of the Constructor and member functions
[8] void read( ), int fact(int) and void compute( ). Also write the main( ) function to
create an object and call the member function accordingly in order to enable the task .
(b) Give any two appropriate differences between recursion and iteration process. [2]