I can\'t figure out how to solve this practice problem. \"write a function num_d
ID: 3614278 • Letter: I
Question
I can't figure out how to solve this practice problem."write a function num_digits(n) that returns the number ofdigits in n. (to determine number of digits, divide it by 10repeatedly.)
this is what I have, but it's not working properly and I knowi have a mistake, just can't figure out where.
#include <stdio.h>
int num_digits(int n) { int i,num; while (num != 0){ i = 0; num /= 10; i++;} printf("%d", i); }
int main(void) { int n; scanf("%d", n); num_digits(n); system("PAUSE"); return 0; }
Thanks for the help!
"write a function num_digits(n) that returns the number ofdigits in n. (to determine number of digits, divide it by 10repeatedly.)
this is what I have, but it's not working properly and I knowi have a mistake, just can't figure out where.
#include <stdio.h>
int num_digits(int n) { int i,num; while (num != 0){ i = 0; num /= 10; i++;} printf("%d", i); }
int main(void) { int n; scanf("%d", n); num_digits(n); system("PAUSE"); return 0; }
Thanks for the help! #include <stdio.h>
int num_digits(int n) { int i,num; while (num != 0){ i = 0; num /= 10; i++;} printf("%d", i); }
int main(void) { int n; scanf("%d", n); num_digits(n); system("PAUSE"); return 0; }
Thanks for the help!
Explanation / Answer
please rate - thanks main problem was i=0; was in the loop, it also wasn't returning thevalue as required #include #include int num_digits(int n) { int i; i = 0; while (n > 0){ n /= 10; i++;} return i; } int main(void) { int n,num; printf("Enter a number: "); scanf("%d", &n); num=num_digits(n); printf("There are %d digits in %d",num,n ); getch(); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.