Wednesday, May 7, 2014

Given a non-negative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops).

public int count7(int n) {
int count=0;
if(n==0){
   return 0;
}
int num = n % 10;
if(num ==7 ){
    count++;
}
n=n/10;
return count+count7(n);

}

No comments:

Post a Comment