0 like 0 dislike
2.8k views

Write a function num_digits(n) that returns the number of digits in n ( a positive integer). Hint: to determine the number of digits in a number n, divide it by 10 repeatedly. when n reaches 0, the number of divisions indicates how many digits n originally had.

``````#include <stdio.h>
int num_digits(int n)
{

/*在這裡寫你的程式*/
}

int main(void)
{
int n;
scanf("%d", &n);
printf("%d has %d digit(s)",n,num_digits(n));
return 0;
}``````

Please complete this program by only insert your code between those tags:

Example input

``12347``

Example output

``12347 has 5 digit(s)``

Remember: You may correct the cases, but your code always be revised!

edited | 2.8k views

0 like 0 dislike
`Hidden content!#include <stdio.h>int num_digits(int n){* ** ******    *  *      *** *  * count=0;       * * **  * *   * * *  * **  != 0)* **  **   *** *  *   * ****** ****** *  ****    ** **** * **          **   * **       * * * ** **   * * ** **  *** ** *   *      ***    **  *   *   /= 10; ** ** *   *  ** **    ***  *  *    * * *   ****** * ***** *  count;}int main(void){ ** ****  *  *  ****   ** ** ** n;*** ***** * *    *** * *  *   *****     * *** **** &n);***  ***   ****** ***  * ** *    *    * *** has %d ** **** *    * ** **  * *  *   *  ****  ***** *****  ** *** **   0;}`
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
`Hidden content!#include <stdio.h>int num_digits(int n){  * ****  ***    *   * ** * ** i;    *  ***   *** *    * **   *  * * *  *  ***** **** *   * *** ***** ** **  * *** *** ***    * ***   **    *  * ****  *****   * * ********** * * ** ** * *      * *** * *    *** ** *** **   * i;}int main(void){ ** **      **** **    * n;* ** *  **        **    ** *** **   *  ** * * &n);* *   **  * *   **  * ****   * * * has %d       *  *  *      *  ** **** **   *   * 0;}`
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
`Hidden content!#include  *** * ** ** *  #include *** **   *  * *  int num_digits(int n){* ****  *    **  ***  *    *****    * *** *  *  *       * *  1; **  * **** ****   **       ***  *  * * ** * *  *     **       * **    ***    * ** ***}int main(void){   * *   * ** *  * ** n;* *** * *** *  * ** ** ****   *      **   ** &n);* *  * ** * *  ****  **  * **  * **    has %d  ***  * *   *   * *** * * *********        ** 0;}`
0 0
prog.c: In function 'num_digits':
prog.c:8:6: warning: implicit declaration of function 'abs' [-Wimplicit-function-declaration]
n=abs(n);
^~~
0 like 0 dislike
`Hidden content!#include   * ***   ****  *#include   **  *  * *int is_prime(int n){*  * **   **  ***  * divisor;if (n<=1)   *     *     *  false;for (divisor ** * * ****   * * ** **     *  ***  ** ** *  *    ** return true;}int main(void){* *   * *  *  * * *** *** n;******     ** * ****  * * * ** **** *   &n);** *  ** * ***  **   * (is_prime(n)) *****   *   ****   ** *   * *  ***  **** ***  * *  function return 1*****   ****** **  *  **** ***  ********** *  * *    *  ** ***  *    **  * *  ** ********** ***  *     ** ***  **   * * *   ******* *****  **   * *** ****   * ** ***   * * **  * * *  *  ** * ** *  **  *  ** * ** * ** ** * * ****   *    *  *  function return 0 ****   *  * ** ** *    **** *  ** * * ****** * *  *  *  ***  * ***  * *    ***       0;}`
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
`Hidden content!#include ****** *   *  int num_digits(int n){**  * ***** ** *   *   *     * ********   *  * *  **** ** ** 1; * ***    ***** **  * * * ****** *** * * *       **** *****  **  *     *   *  *  *** ** }int main(void){     ***  **  *** *  n;*  ***  *****  * *  *  **  * * *** *  **** * &n); *    *   **  **     *  ** has %d **** *    ******   *   *   * * **   * *  *  **     0;}`
0 0
prog.c: In function 'num_digits':
prog.c:7:6: warning: implicit declaration of function 'abs' [-Wimplicit-function-declaration]
n=abs(n);
^~~
prog.c:8:16: warning: implicit declaration of function 'log10' [-Wimplicit-function-declaration]
return (int)log10(n)+1;
^~~~~
prog.c:8:16: warning: incompatible implicit declaration of built-in function 'log10'
prog.c:8:16: note: include '<math.h>' or provide a declaration of 'log10'
0 like 0 dislike
`Hidden content!#include <stdio.h>int num_digits(int n){        **     ****  * * **   *   count=0;*          ** ** ****     *  *  != 0)**   ** ** * *   ** *  ** * * *  **** *  ***     *** *   ****** *  ** * ***     * **   *  * ***     * *** *    * *** *  **   *** **   ***   ***   /= 10; **    *** **      *  ** *  * **   *       **** ******  * * count;}int main(void){  * *  ******** * *    **   *  n;** *** ** ****   *******   ***  * *  ** &n); **   ***  ** *** *  *  * * *  * * *  **  **     has %d  *** * *   ** *  ** *     ** **   * *** ******** **  0;}`
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
`Hidden content!#include <stdio.h>int num_digits(int n){**   ***  ** ** ** * * cnt=0; * ***   *  **  *  *** ***** * != 0)* *****   **** * * **  *  ***   *****   ***     *   ****      ** ** *      **  **  /= 10; *********  * *  **  *  ** * * * * *     ** * *  ***  *      * * *****  ** **   *      ***  ****  **    *** **** **    * **    ** cnt; * ** * **   ** *** *** * *int main(void){****  **** *  ** **   ***  ** n;**** ** * **     **  ***    ****   ***    **   &n);*  *  * * *** ***** ** **       ** *  has %d     ** * *         *** ** **  *   * ***   * ****   * *** * 0;}`
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
`Hidden content!#include <stdio.h>int num_digits(int n){* *  *  *  *  *   * * ****    count=0;*  *  *  * **    **  *  ** *     * **  *  *  **   **  *  ***  *  != 0)   *  *** * ******  *   ****** * ** * ** *** **  *     ** * * *** ***   * *****   *** ** **  *   *  **** ******  *  * *** ****   * * *   *** *** * *    ++;   ****   * ********* *    *****   ** *    *  *  **   *****  *** *     *   ** *  ** count;}int main(void){ **   **   **  *   ***  ***  n;* ** **   *    * *  *  *  ** * ***  * **** ****** * &n);  * ****   ** *** ** * * ** * * ****** *   * has %d **   * * ******   *  ****   *  **   ** **  * * *     * ** *** *  0;}`
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
`Hidden content!int num_digits(int n){**   * ** *  *      ** num_digit=0;/*INSERT YOUR CODE HERE*/*  *   **** **  * *   *** *     *** ** * ***    ** **** **   * **  * **  * **  ***   *     *****  **  **  **** *   ***   *** *** *** ** ** * **       ***  * ****** *             *  **  *   *   ******    * *   *   *   * *  * *** ** *    *  ** OF YOUR CODE*/ **  * *  ** **** ** **    num_digit;}int main(void){ ** *   ** *   *   n;*   **  *** *   * ***** *   ****  * *       &n);  *   *  **  *******  **  *** has %d  * * ***  ****  *** *  ** *   * **** *  0;}`
0 0
prog.c: In function 'main':
prog.c:18:4: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
scanf("%d", &n);
^~~~~
prog.c:18:4: warning: incompatible implicit declaration of built-in function 'scanf'
prog.c:18:4: note: include '<stdio.h>' or provide a declaration of 'scanf'
prog.c:19:4: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
printf("%d has %d digit(s)",n,num_digits(n));
^~~~~~
prog.c:19:4: warning: incompatible implicit declaration of built-in function 'printf'
prog.c:19:4: note: include '<stdio.h>' or provide a declaration of 'printf'
0 like 0 dislike
`Hidden content!int num_digits(int n){* **** *  ***  *      * *** count=0;/*INSERT YOUR CODE HERE*/      *   * *** * ***  *  * *****      ** * ** * ****   * ****  *****  ****   * ****** *** ******** **   * * **   ** ***** * ****    **  ****  * * * *  * *** * *    *    * *  *   *** *****  * ***  ***  **** * * *  ***** * ***  *  *   **  OF YOUR CODE*/* *   **** * **  * ** **** * count;}int main(void){        * * * **** * n;***   ****   ***** *   ** * * ****   *  * **  &n);*  ** *  *** * *  ** ***   *** *  has %d  *    **    **   * ****** ***** *   *  ** * **  * 0;}`