© Copyright Reserved by Debanjan Gupta
HOMEWORK - RECURSION I
QUESTION 1: PREDICT THE OUTPUT:
a) 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 by someFun(24,2) ?
ii) What will be returned by someFun(84, 2)?
iii) State in one line what does someFun() does apart from recursion?
b) int task (int m, int n){
if (m == n)
return m;
else if (m > n)
return (m-n, n);
else
return (m, n-m);
}
i) What will function task() return if m=30 and n =45?
ii) What does function task() perfrom, apart from recursion?
c) 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);
}
}
What will function Mystery() return when the value of num=43629, x= 3 and y=4
respectively? Show dry run/working.
© Copyright Reserved by Debanjan Gupta
QUESTION 2 : CODING
a) Design a class ArmNum to check if a number if an Armstrong Number or not.
[A number is said to be Armstrong if the sum of its digits raised to the power of length
of the number is equal to the number]
Example: 1634 = 14 + 64 + 34 + 44 = 1634
Data Members/Instance Variables:
n : to Store the number
l : to store the length of the number.
Member Methods:
ArmNum(int nn) = Parameterized constructor to initialize n=nn
int sum_pow(int i) = return 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 or
not by invoking sun_pow( ) and displays and appropriate
message.
Define main(), create object and invoke the above functions.
b) Design a class Palin to check if a number is Palindrome or not.
[A number is said to be Palindrome if its same as its reverse]
Data Members:
num : to store the number
revnum : to store the reverse of the number
Member Methods:
Palin() : constructor to initialize data members with legal default values.
void accept() : To accept a number
int reverse(int y) : Reverses the number passed as an argument and stores it in
‘revnum’ using recursive technique.
void check() : Checks if the number is Palindrome or not by invoking function
reverse()
Define main(), create object and invoke the above functions.
*********************************************************************
Directions:
• The source code of the programs must be written in your notebooks and thesame must
be done practically in BlueJ
• The programs written in BlueJ must be executed with a sample input, and the screenshot of
the output must be submitted in PDF \ JPG format along with thesource code.
• The Homework must be submitted before your respective classes.
• All the answers must be submitted in personal whatsapp message and not inthe group.