Advertisements
Advertisements
Question
The following function is a part of some class which is used to find the smallest digit present in a number. There are some places in the code marked by ?1?, ?2?, ?3? which must be replaced by an expression/a statement so that the function works correctly.
int small_dig(int n)
{ int min=?1?;
while(n!=0)
{
int q=n/10;
int r=?2?*10;
min=r>min ? ?3? : r;
n=q;
}
return min;
}
- What is the expression or statement at ?1?
- What is the expression or statement at ?2?
- What is the expression or statement at ?3?
Answer in Brief
Solution
- n
- n%10 + 0
- min
int small_dig(int n)
{
int min=n;
while(n!=0)
{
int q=n/10;
int r=n%10+0*10;
min=r>min?min:r;
n=q;
}
return(min);
}
shaalaa.com
Looping (For, While-do, Do-while, Continue, Break)
Is there an error in this question or solution?