write a function num_digits (n) that returns the number of digits in apositive integer n.
write a function digit (n,k) that returns the kth digit from the right in the positive integer n. for example, digit (829, 1)returns 9, digit (829, 2) returns 2, and digit (829, 3) returns 8. If k is greater that the number of digits in n, have the function return -1.
Thanks for your help
You could do both of these a number of ways. For example, For the first, you could either use a iteration loop or a recursive loop to divide the number by 10 and keep track of the number of times you need to execute the loop. The second could be done also by dividing by 10 a number of times, and then using the modulus operator (%).
You can get references on the C language here:
[lysator.liu.se ]
The original classic (pre ANSI C) is:
[cm.bell-labs.com ]
Sorry if the above is cryptic. Its just that its not really an advanced C question, so I'm not sure what level of knowledge I should assume. i.e. Are you having trouble with the syntax of how to write functions in C, or the algorithm to use, or getting what you've done to compile, or what? Tell us what you've tried and the error messages you got, and I'll try to help more. (Is it for a course assignment?)
Shawn