Page 441 - Computer science 868 Class 12
P. 441
(a) What will the function task() return when the value of m = 30 and n = 45? 2
Ans. Dry run
task(30, 45) -> task(30, 15) -> task(15, 15) -> 15
It will return 15
(b) What function does task() perform, apart from recursion?
Ans. Returning the HCF of two numbers, m and n
2. Design a class NumDude to check if a given number is a Dudeney number or not. [ISC 2023]
(A Dudeney number is a positive integer that is a perfect cube, such that the sum of its digits is equal to the cube root of the
number.)
Example: 5832 = (5 + 8 + 3 + 2)3 = (18)3 = 5832
Some of the members of the class are given below:
Class name : NumDude
Data member/instance/variable:
num : to store a positive integer number
Methods/Member functions:
NumDude() : default constructor to initialise the data member with legal initial
value
void input() : to accept a positive integer number
int sumDigits(int x) : returns the sum of the digits of number ‘x’ using recursive technique
void is Dude() : checks whether the given number is a Dudeney number by
invoking the function sumDigits() and displays the result with an
appropriate message
Specify the class NumDude giving details of the constructor(), void input(), int sumDigits(int) and void is Dude().
Define a main() function to create an object and call the functions accordingly to enable the task.
Ans. import java.util.Scanner;
class NumDude
{
int num;
public NumDude()
{
num=0;
}
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no");
num=sc.nextInt();
}
public int sumDigits(int x)
{
if(x==0)
return 0;
else
return x%10+sumDigits(x/10);
}
public void isDude()
{
int n=sumDigits(num);
if(n*n*n==num)
System.out.println("Dudeney number");
else
System.out.println("Not a Dudeney number");
}
public static void main(String args[])
{
NumDude obj=new NumDude();
439
Recursion 439

