## Problem:

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). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).

count7(717) → 2
count7(7) → 1
count7(123) → 0

## Solution:

public int count7(int n) {
if (n < 1) return 0;
if (n % 10 == 7) return 1 + count7(n/10);
else return count7(n/10);
}

1. can you explain this ?

2. int count=0;
if(n<1) return 0;
if(n%10==7) count++;
return count +count7(n/10);

1. count just switches back to 0 each method call...unne unnecessary memory usage

3. How can I do this with while loop?

4. public int count7(int n) {
if(n == 0) {
return 0;
}

if(n % 10 == 7) {
return 1 + count7(n / 10);
} else {
return count7(n / 10);
}
}

5. static int countOf7s=0;
public static int getCountOf7s(int intVal) {
while(intVal>6){
if(intVal==7 || intVal%10==7) {
countOf7s++;
}
intVal=intVal/10;
}
return countOf7s;
}

6. int count = 0;
while (n != 0) {
if (n % 10 == 7) {count++;}
n /= 10;
}return count;