0% found this document useful (0 votes)
4 views3 pages

Recursive Programs

Uploaded by

leena9804
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)
4 views3 pages

Recursive Programs

Uploaded by

leena9804
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

Homework :

1)

2) The following program code checks if the positive integer ‘N’ is a palindrome number by returning true
or false. There are some places in the code marked as ?1?, ?2?, ?3?, ?4? and ?5? which are to be replaced
by a statement/expression so that the code works properly.
boolean Palindrome(int N)
{
int rev = ?1? ;
int num = N;
while (num>0)
{ int f = num/10;
int s = ?2? ;
int digit = num − ?3? ;
rev = ?4? + digit;
num /= ?5?;
}
if(rev == N)
return true;
else
return false;
}
Answer the following question:
(a) What is the statement or expression at ?1? [1]
(i) –1 (ii) 0 (iii) 10 (iv) 2
(b) What is the statement or expression at ?2? [1]
(i) s *10 (ii) f /10 (iii) rev (iv) f *10
(c) What is the statement or expression at ?3? [1]
(i) s (ii) rev (iii) f (iv) digit * 10
(d) What is the statement or expression at ?4? [1]
(i) s * 10 (ii) rev *10 (iii) f (iv) rev
(e) What is the statement or expression at ?5? [1]
(i) 1 (ii) 100 (iii) 10 (iv) rev
3)

4) Design a class ArmNum to check if a given number is 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:
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:
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
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.

5) 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:
Consider the number 28.
22 + 82 = 4 + 64 = 68
62 + 82 = 36 + 64 = 100
12 + 02 + 02 = 1.
Hence, 28 is a happy number.
Now consider the number 12.
12 + 22 = 1 + 4 = 5.
Hence, 12 is not a happy number.
Design a class Happy to check if a given number is a happy number. Some of the 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.
void getNum(int num): to assign the parameter value to the number n = num.
int sumSquareDigits(int x): returns the sum of the square of the digits of the number x, using the recursive
technique.
void isHappy(): checks if the given number is a happy number by calling the function sumSquareDigits(int)
and displays an appropriate message.
Specify the class Happy, giving details of the functions. Also define main() function to create an object and
call the methods to check for happy number.

6) 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. [10]
Example: 135 = 11 + 32 + 53
Hence, 135 is a disarium number.
Design a class Disarium to check if 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 number(n) to the power of their respective
positions (p) using recursive technique
void check(): checks whether the number is a disarium number and displays the result with an appropriate
message
Specify the class Disarium giving the details of the constructor! ), void countDigit(), int sumofDigits(int, int)
and void check(). Define the mainO function to create an object and call the functions accordingly to enable
the task.

You might also like