0 喜歡 0 不喜歡
12.1k 瀏覽

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!

[練習] Coding (C) - 最新提問 分類:Chapter 9: Functions | 用戶: (5.2k 分)
ID: 35784 - 從幾時開始: 2017-12-07 18:00 - 到幾時結束: 無限制

修改於 用戶: | 12.1k 瀏覽

45 個回答

0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
int num_digits(int n)
{
** ** ** * * * * *** count=0;
*** **** * *** *** * ***** != 0)
* **** * ** *
* ** ** * ****** * **** ***** * * ** * ** ****** **
* * * ** * * *** *** * * ** * * ** * ** /= 10;
**** * * **** ** ** ** *
* * * * **** ******** ** count;
}

int main(void)
{
***** * *** * * ** ** * **** * n;
* * *** ** *** * ** ** * * ** * * &n);
* **** **** * * ** * * ******* has %d * * * * * * *****
*** * ** ** * ** ** * 0;
}
最新回答 用戶: (-140 分)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
int num_digits(int n)
{
* * * * * ** * * ** * *** i;
*** * **** * ** ** * ** ** ** ***
** ** * * *** *** * ** ****
* *** * *** **** *** ****** ** * * * * * *** *
*** * ** *** **** ** ***
* ** ** ** * *** ********* i;
}

int main(void)
{
** * *** ** * n;
* *** ***** * ** * **** * &n);
* ****** * ***** * * **** has %d *** * **** * * * *
***** * * ** ** * * 0;
}
最新回答 用戶: (-66 分)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 喜歡 0 不喜歡
內容已隱藏
#include *** * *
#include ** * *** * *
int num_digits(int n)
{
*** ***** * ***
***** ****** * * * ****** * 1;
** * * * **** ***
***** *** * ** * ****
** * *** ** ** * *** * ** * *
}



int main(void)
{
** * *** * * *** ** n;
* * * *** * **** * * * *** &n);
** * * * ****** * * ** * has %d * ** * ***** ** ***
* * * ******* *** * ** 0;
}
最新回答 用戶: (16 分)
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 喜歡 0 不喜歡
內容已隱藏
#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;
}
最新回答 用戶: (16 分)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 喜歡 0 不喜歡
內容已隱藏
#include * **
int num_digits(int n)
{
*** **** ** * * ** *** *
*** * ** **** ******* * ** 1;
* * ** *** *
** *** * ** * *
** * ** * ****** * ** * * * *
}

int main(void)
{
* * * *** n;
* * * *** ** * ** * * **** &n);
* * * * * *** * * ** * * * * has %d ** **** * * **
*** * * *** * * * * 0;
}
最新回答 用戶: (16 分)
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 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
int num_digits(int n)
{
* ** ** ***** *** count=0;
** *** * **** *** * != 0)
** *** * * * ** * **
** ** ** * * *** ***** * ** ********* ** ** * *
* * ** * * **** ** * * **** ** *** *** *** ** /= 10;
* *** ** * *** * **
* * ** **** * * * * ** * count;
}

int main(void)
{
***** * * * *** * ** * n;
*** * * * * ** * ** ***** ** &n);
* *** ** ** ***** ** * * has %d ***** * * * * *****
****** *** * * * ** * **** 0;
}
最新回答 用戶: (-168 分)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
int num_digits(int n)
{
** * * ***** * * * ** cnt=0;
* * * * * ** ** ***** ** * != 0)
* * ****** * ** * * ** ** **
* ** **** * * ** * * * * * * * * * *** ** /= 10;
** * * ** ** * * *** ** ** **
* * * * * *** *
** * ** * * *** ***** cnt;
* * ** ** * * * * * **

int main(void)
{
*** * * * * ** **** * * ** ** n;
* *** * *** * * * * *** ** *** ***** * &n);
** ** * * *** * * * * ** *** ** has %d ** *** * ****
*** *** ***** ** ** ** 0;
}
最新回答 用戶: (323 分)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
int num_digits(int n)
{
*** ** ** * *** * count=0;
*** **** * *** *
**** * ** * ****** != 0)
* * * **** * * ** * **** * * **
* * * ** * * * ** * * * *** ** ***** *
* * ****** * ***** * **** * * * * ** * ++;
** * *** ******** *
****** ***** **** **
* ** ** * * **** * ** **** count;
}
int main(void)
{
***** *** * ** * ** * n;
** ** *** * * * **** ** *** * * ** ****** &n);
* ** *** * * * *** * * *** ** * * has %d *** * * * **
* * * * * * * * ** 0;
}
最新回答 用戶: (-193 分)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 喜歡 0 不喜歡
內容已隱藏
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;
}
最新回答 用戶: (-167 分)
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 喜歡 0 不喜歡
內容已隱藏
int num_digits(int n)
{
* ** ** * * ** count=0;/*INSERT YOUR CODE HERE*/
* * **** ** * * * * *
*** ** *** * ** **
* * * * *** ** * ** * * * **** * ***** ** * * **
* *** * *** **** * *** *** * * * * * * * * *
* * *** ** * * *

* **** *** **** * ***
** ****** *** *** * OF YOUR CODE*/
* * ** * **** **** count;
}

int main(void)
{
** ** * *** *** * n;
* * * * **** * ** * * * * ** * * &n);
** * ******* * ******** *** * has %d ** * ***** * ** ******
* * * ** * * 0;
}
最新回答 用戶: (-167 分)
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

相關問題

0 喜歡 0 不喜歡
47 回答
[練習] Coding (C) - 最新提問 12月 7, 2017 分類:Chapter 9: Functions | 用戶: semicolon (5.2k 分)
ID: 35785 - 從幾時開始: 2017-12-07 18:00 - 到幾時結束: 無限制
| 13k 瀏覽
0 喜歡 0 不喜歡
79 回答
[練習] Coding (C) - 最新提問 12月 28, 2017 分類:Chapter 9: Functions |
ID: 39986 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 24.3k 瀏覽
0 喜歡 0 不喜歡
70 回答
[練習] Coding (C) - 最新提問 12月 7, 2017 分類:Chapter 9: Functions | 用戶: semicolon (5.2k 分)
ID: 35783 - 從幾時開始: 2017-12-07 18:00 - 到幾時結束: 無限制
| 17.9k 瀏覽
0 喜歡 0 不喜歡
50 回答
[練習] Coding (C) - 最新提問 12月 13, 2017 分類:Chapter 9: Functions | 用戶: semicolon (5.2k 分)
ID: 37278 - 從幾時開始: 2017-12-14 18:00 - 到幾時結束: 無限制
| 15.9k 瀏覽
12,783 問題
183,442 回答
172,219 留言
4,824 用戶