// 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
#include <stdio.h>
// To enable debug messages uncomment #define
#define TEST 1
int count7(int n);
void startTesting();
int main(void) {
#ifdef TEST
startTesting();
#endif
return 0;
}
int count7(int n) {
if (n == 7) {
return 1;
} else if (n / 10 <= 0) {
return 0;
}
if (n % 10 == 7) {
return 1 + count7(n / 10);
} else {
return count7(n / 10);
}
}
void startTesting()
{
int result = 0;
int i = 0;
for (i = 115; i <= 125; i++) {
result = count7(i);
printf("Count7(%d) = %d\n", i
, result
); }
}