0 like 0 dislike
12.1k 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.

寫一個num_digits(n)函數 計算n有幾位數

#include <stdio.h>
int num_digits(int n)
{
   /*INSERT YOUR CODE HERE*/

   /*在這裡寫你的程式*/
   /*END OF YOUR CODE*/
}

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:

   /*INSERT YOUR CODE HERE*/ 
   /*END OF YOUR CODE*/

Example input

12347

Example output

12347 has 5 digit(s)

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

[Exercise] Coding (C) - asked in Chapter 9: Functions by (5.2k points)
ID: 35784 - Available when: 2017-12-07 18:00 - Due to: Unlimited

edited by | 12.1k views

45 Answers

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;
}
answered by (-140 points)
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;
}
answered by (-66 points)
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;
}
answered by (16 points)
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;
}
answered by (16 points)
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;
}
answered by (16 points)
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;
}
answered by (-168 points)
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;
}
answered by (323 points)
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;
}
answered by (-193 points)
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;
}
answered by (-167 points)
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;
}
answered by (-167 points)
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'
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:104.23.197.64
©2016-2026

Related questions

0 like 0 dislike
47 answers
[Exercise] Coding (C) - asked Dec 7, 2017 in Chapter 9: Functions by semicolon (5.2k points)
ID: 35785 - Available when: 2017-12-07 18:00 - Due to: Unlimited
| 13k views
0 like 0 dislike
79 answers
[Exercise] Coding (C) - asked Dec 28, 2017 in Chapter 9: Functions
ID: 39986 - Available when: Unlimited - Due to: Unlimited
| 24.3k views
0 like 0 dislike
70 answers
[Exercise] Coding (C) - asked Dec 7, 2017 in Chapter 9: Functions by semicolon (5.2k points)
ID: 35783 - Available when: 2017-12-07 18:00 - Due to: Unlimited
| 17.9k views
0 like 0 dislike
50 answers
[Exercise] Coding (C) - asked Dec 13, 2017 in Chapter 9: Functions by semicolon (5.2k points)
ID: 37278 - Available when: 2017-12-14 18:00 - Due to: Unlimited
| 15.9k views
12,783 questions
183,442 answers
172,219 comments
4,824 users