HW - Recursion 1
HW - Recursion 1
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);
}
}
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
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.
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.