#include <iostream>
int first_digit( unsigned int number )
{
std::cout << "number: " << number << '\n' ;
if( number < 10 ) // the number consists of a single digit
{
std::cout << "there is just one digit, return it\n" ;
return number ; // just return that digit
}
// the number consists of two or more digits
// number/10 gives the number with the last (right-most) digit removed
return first_digit( number/10 ) ; // call recursively with one less digit
// each time through the recursion, we reduce the number of digits by one.
// Eventually, when there is just one digit left,
// that is the first (left-most) digit and we return that
}
int main()
{
std::cout << first_digit(12345678) << '\n' ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKaW50IGZpcnN0X2RpZ2l0KCB1bnNpZ25lZCBpbnQgbnVtYmVyICkKewogICAgc3RkOjpjb3V0IDw8ICJudW1iZXI6ICIgPDwgbnVtYmVyIDw8ICdcbicgOwoKICAgIGlmKCBudW1iZXIgPCAxMCApIC8vIHRoZSBudW1iZXIgY29uc2lzdHMgb2YgYSBzaW5nbGUgZGlnaXQKICAgIHsKICAgICAgICBzdGQ6OmNvdXQgPDwgInRoZXJlIGlzIGp1c3Qgb25lIGRpZ2l0LCByZXR1cm4gaXRcbiIgOwogICAgICAgIHJldHVybiBudW1iZXIgOyAvLyBqdXN0IHJldHVybiB0aGF0IGRpZ2l0CiAgICB9CgogICAgLy8gdGhlIG51bWJlciBjb25zaXN0cyBvZiB0d28gb3IgbW9yZSBkaWdpdHMKICAgIC8vIG51bWJlci8xMCBnaXZlcyB0aGUgbnVtYmVyIHdpdGggdGhlIGxhc3QgKHJpZ2h0LW1vc3QpIGRpZ2l0IHJlbW92ZWQKCiAgICByZXR1cm4gZmlyc3RfZGlnaXQoIG51bWJlci8xMCApIDsgLy8gY2FsbCByZWN1cnNpdmVseSB3aXRoIG9uZSBsZXNzIGRpZ2l0CgogICAgLy8gZWFjaCB0aW1lIHRocm91Z2ggdGhlIHJlY3Vyc2lvbiwgd2UgcmVkdWNlIHRoZSBudW1iZXIgb2YgZGlnaXRzIGJ5IG9uZS4KICAgIC8vIEV2ZW50dWFsbHksIHdoZW4gdGhlcmUgaXMganVzdCBvbmUgZGlnaXQgbGVmdCwKICAgIC8vIHRoYXQgaXMgdGhlIGZpcnN0IChsZWZ0LW1vc3QpIGRpZ2l0IGFuZCB3ZSByZXR1cm4gdGhhdAp9CgppbnQgbWFpbigpCnsKICAgIHN0ZDo6Y291dCA8PCBmaXJzdF9kaWdpdCgxMjM0NTY3OCkgPDwgJ1xuJyA7Cn0K