Advertisements
Advertisements
Question
The following function duck () is a part of some class which is used to check if a given number is a duck number or not. There are some places in the code marked by ?1?, ?2?, ?3? which may be replaced by a statement/expression so that the function works properly.
A number is said to be Duck if the digit zero (0) is present in it.
boolean duck(int a)
{ int f=−1;
if(a==0)
return true;
for(int i=a; i!=0;?1?)
{ int c = i%10;
if(c==?2?)
{ f=1; break; }
}
return (f==?3?)? false:true;
}
- What is the expression or statement at ?1? [1]
- What is the expression or statement at ?2? [1]
- What is the expression or statement at ?3? [1]
Long Answer
Advertisements
Solution
- The for loop processes the number digit by digit. At each step, the last digit is removed by performing integer division by 10. Therefore, the value is updated as i = i/10.
- The function checks whether any digit of the number is zero. Since the presence of zero in the digits classifies a number as a duck number, the required condition is to check for 0.
- If a 0 is found, the function assigns f = 1. If no zero occurs, the value of f remains −1. When no zero is detected (f == −1), the function returns false; otherwise, it returns true.
shaalaa.com
Is there an error in this question or solution?
2024-2025 (March) Official Board
