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

12th Recursion function Assignments Year Wise_24708739

Important

Uploaded by

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

12th Recursion function Assignments Year Wise_24708739

Important

Uploaded by

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

JAVA MATLAB RAMJI SIR : 9305035029

12th Recursion Function Assignments – 2022


Question 2020

Design a class BinSearch to search for a particular value in an array.Some of the members
of the

class are given below:

Class name : BinSearch

Data members/instance variables:

arr[] : to store integer elements.

n : integer to store the size of the array.

Member functions/methods:

BinSearch(int num) : parameterized constructor to initialize n = num.

void fillArray() : to enter elements in the array.

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

technique and returns its location if found otherwise

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

Design a class ArmNum to check if a given number is an Armstrong number or 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]

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.

Some of the members of the class are given below: [10]

Class name : ArmNum

Data members/instance variables:

n : to store the number

l : to store the length of the number

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

of the number using recursive technique. Eg. 34 will return 32

+ 42 (as the length of the number is 2)

void isArmstrong( ) : checks whether the given number is an Armstrong number by


invoking the function
JAVA MATLAB RAMJI SIR : 9305035029
sum_pow( ) and displays the result with an appropriate message.

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

functions accordingly to enable the task.

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]

Example : 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of 6, excluding itself)

Some of the members of the class are given below: [10]

Class name : Perfect

Data members/instance variables:

num : to store the number

Methods/Member functions:

Perfect (int nn) : parameterized 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 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.

Some of the members of the class are given below: [10]

Class name : Palin

Data members/instance variables:

num : integer to store the number

revnum : integer to store the reverse of the number

Methods/Member functions:

Palin( ) : constructor to initialize data members with legal initial values

void accept( ) : to accept the number

int reverse(int y) : reverses the parameterized argument ‘y’ and stores it in ‘revnum’

using recursive technique

void check( ) : checks whether the number is a Palindrome by invoking the function

reverse( ) and display the result with an appropriate message.


Specify the class Palin giving the details of the constructor( ), void accept( ), int reverse( int ) and void

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]

respective position is equal to the number itself

Example: 135 = 11 + 32 + 53

Hence, 135 is a disarium number.

Design a class Disarium to check 1f a given number is a 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
Methods/Member functions:
Disarium(int nn) : parameterized constructor to initialize the data
members n = nn and size = 0
void countDigit( ) : counts the total number of digits and assigns it
to size.
int sumofDigits(int n, int p) : returns the sum of the digits of the nwnber(n) to the
power. of their respective positions(p) using recursive
technique.
void check( ) : checks whether the number is a disarium
JAVA MATLAB RAMJI SIR : 9305035029
number and displays the result with an appropriate message.
Specify the class Disarium giving the details of the constructor( ), void countDigit( ),
int sumoIDigits(int, int) and void check(). Define the main() function to create an object
and call the functions accordingly to enable the task.
Question 2015.

A class Admission contains the admission numbers of 100 students. Some of the data [10]

members I member functions are given below:

Class name Admission

Data member/instance variable:

Adno[] : integer array to store admission numbers

Member functions/methods:

Admission( ) : constructor to initialize the array elements

void fillArray( ) : to accept the elements _ of the array in ascending order

int binSearch(int I, int u, int v) : to search for a particular admission number (v)

using binary search and recursive technique and returns 1

if found otherwise return -1.

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

functions accordingly to enable the task.


JAVA MATLAB RAMJI SIR : 9305035029

Question 2014.

A class SeriesSum is designed to calculate the sum of the following series:

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

members of the class are given below:

Class name Emirp

Data members/instance variables:

n : stores the number

rev : stores the reverse of the number

f : stores the divisor

Member functions:

Emirp(int nn) : to assign n = nn, rev = 0 and f = 2

int isprime(int x) : check if the number is prime using the recursive technique and return 1

if prime otherwise return 0.

void isEmirp( ) : reverse the given number and check if both the original number and

the reverse number are prime by invoking the function isprime(int)

and display the result with an appropriate message.


JAVA MATLAB RAMJI SIR : 9305035029
Specify the class Emirp giving details of the constructor(illt), int isprime(int) and void

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

n : stores the number

Data members/instance variables:

Member functions:

Happy( ) : constructor to assign 0 to n

void getnum(int nn) : to assign the parameter value to the number n = nn

int sum_sq_digits(int x) : returns the sum of the square of the digits of the number x, using

the recursive technique.


JAVA MATLAB RAMJI SIR : 9305035029
void ishappy( ) : checks if the given number is a happy number by calling the

function sum_sq_digits(int) and displays an appropriate message

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:

Class name : DeciOct

Data Members / instance variables:

n : stores a decimal number

oct : stores a octal equivalent number

Member functions:

DeciOct( ) : constructor to initialize the data members n=0, oct=0.

void getnum(int nn ) : assign nn to n.

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

display its octal equivalent.

(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.

(b) State any two disadvantages of using recursion. [2]

Question 2010.

Design a class Change to perform string related operations. The details of the class are
given below:

Class name : Change

Data members

str : stores the word

newstr : stores the changed word

len : stores the length of the word

Member functions:

Change() : default constructor

void inputword() : to accept a word

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.

void display() : displays both the words.

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:

Class name : Binary

Data members :

A[] : integer array of 100 elements

n : size of array

l : location of the lower bound

u : location of the upper bound

Member functions:

Binary(int nn) : constructor to initialize the size n to nn and the other instance

variables.

void readdata() : to fill the elements of the array in ascending order.

int binary_search(int v) : returns the location of the value(v) to be searched in the list by

binary search method using the recursive technique. The

function returns -1 if the number is not present in the given list.

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:

Class name : Revstr

Data members

Str : stores the string.

Revst : stores the reverse of the string.

Member functions

void getStr() : to accept the string.

void recReverse(int) : to reverse the string using recursive technique.

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:

Class name : Convert

Data members

n : integer whose digits are to be expressed in words.

Member functions

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 recursive technique.

void num_to_words(int) : to display the digits of an integer in words.

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:-

Class name : Hifact

Data members

A : to input first integer

B : to input second integer

hcf : : to store hcf of two integers

lcm : to store lcm of two integers

Member functions

Hifact() : constructor to assign initial values to 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 recursive technique

int fn_lcm(int,int,int) : to find lcm using a,b and hcf

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:

Class Name : recursion

Data Members

limit : int

Member functions

recursion() : constructor to assign a,b,c with appropriate values.

void input() : to accept the limit of the series.

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

write the main function. [8]

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:

Class name : Special

Data members /instance variables :

n : integer to store the number

Member functions :

Special( ) : default constructor

void read( ) : to accept the number

int factorial(int x) : return the factorial of a number using recursion technique.

boolean isSpecial( ) : checks for the special number by invoking the function

factorial( ) and returns true if Special, otherwise returns false

void display( ) : to show the result with an appropriate message.

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.

The function can be computed from the formula:


n!
C( n,k ) =
k!(n–k)!
Design a class Combination to implement this formula. Some of the data members and member functions
are given below.

Class name : Combination

Data members/instance variables :

n : integer number

k : integer number

Member functions :

Combination ( ) : to initialize the data members n = 0 and k = 0

void read( ) : to accept the value of the data members

int fact(int) : return the factorial of a number using Recursion Technique.

void compute( ) : calculate and display the result

(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]

You might also like