English

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 - Computer Science (Theory)

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;
} 
    
  1. What is the expression or statement at ?1?
  2. What is the expression or statement at ?2?
  3. What is the expression or statement at ?3?
Answer in Brief

Solution

  1. n
  2. n%10 + 0
  3. 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?
2023-2024 (February) Official
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×