/**********************************************
The function length(int n) is a recursive
function that returns the number of characters
needed to print out n.
**********************************************/
#include <stdio.h>
int length(int);
int main() {
// Get integer n
int n;
printf("Enter integer: ");
fflush(stdout);
scanf(" %i", &n);
// Print out number of characters in n
printf("Printing %i takes %i characters.\n", n, length(n));
return 0;
}
int length(int n) {
if (n < 0) {
return 1 + length(-n); // negatives!
} else if (n < 10) {
return 1; // single digits! (base case)
} else {
return 1 + length(n/10);// multiple digit positive!
}
}