Tuesday, November 26, 2013

WAP to count the number of digits in a number using recursion

QUESTION:
Write a program to count the number of digits in a number using recursion 

CODE:
class CountingDigits
  { // Start of class
    public int count(int num) 
     { 
       int c=0;  
       if(num==0) 
        { //If the number is 0, then number of digits in number is 0
        return(0); 
        }
       else 
        {
        ++c;
        return(c+count(num/10)); // Recursive call 
        }
     }
  } //End of class


No comments:

Post a Comment