0 thích 0 k thích
9.5k đã xem

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) - đã hỏi trong Chapter 9: Functions bởi (5.2k điểm)
ID: 35784 - Xem được từ: 2017-12-07 18:00 - Hiệu lực đến: Không giới hạn

đã sửa bởi | 9.5k đã xem

45 Trả lời

0 thích 0 k thích
Hidden content!
#include <stdio.h>
int num_digits(int n)
{
** * * * **** * * ******** count=0;
*** * * * ***** * * * ** != 0)
**** ****** *** ******
** * *** * * * ** * * ***** * * ** * ** * * ******* *
* * ** ** *** ** *** ** * * * * * ** ** ** ** /= 10;
** ** **** * **** *** ** *
**** ** * * *** *** count;
}

int main(void)
{
*** * * * ** * *** * *** n;
*** * ** * * ** ** * * * * ** * * * &n);
**** ** * ** * * * * * ****** has %d ** * ** * ** ** *
* * * ** ******* * **** 0;
}
trả lời bởi (-140 điểm)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 thích 0 k thích
Hidden content!
#include <stdio.h>
int num_digits(int n)
{
* **** *** *** * * * i;
* *** * * *** ** * ** ** *********
* *** **** * * * **
* * **** ** * ** * *** ********* ** ** * *
*** * **** * ** * *
*** * ** *** *** ** * **** i;
}

int main(void)
{
** * * ** * n;
* * ***** * ** * ** ** * * &n);
** * ** *** *** * * * has %d ** ** ** ** * * **
** * * ***** ** * *** * * 0;
}
trả lời bởi (-66 điểm)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 thích 0 k thích
Hidden content!
#include ** ** *
#include ** ** ***
int num_digits(int n)
{
*** * * ** * * * ****
** * ** * ** * **** ***** *** 1;
* * * *
**** * * ** * * ** ** *
** **** * ** ** ** * ** ** *
}



int main(void)
{
*** * *** * * ** * * n;
* * * * * * * * ** ** *** &n);
* ** * * ** * ** * ** *** * has %d ** ** ** * * ****
* * ** ** **** ** * 0;
}
trả lời bởi (16 điểm)
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 thích 0 k thích
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;
}
trả lời bởi (16 điểm)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 thích 0 k thích
Hidden content!
#include * * **** * ***
int num_digits(int n)
{
* ** ** * * *** *
* ** ** ** * * ** * 1;
* * ** *** *
** * * * * ** *
** * *** ** ** * ** **** *
}

int main(void)
{
* ** ** ** * n;
* * * * *** **** * * ** ** * ** &n);
* * ** * * * ***** * has %d * ***** * * ***
* * * * **** *** * * * 0;
}
trả lời bởi (16 điểm)
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 thích 0 k thích
Hidden content!
#include <stdio.h>
int num_digits(int n)
{
* ** * * * ** * ** count=0;
** ** * * ** *** **** * != 0)
* * * * *** * **** * ****
** * ***** * * ** *** ** *** ***** * *
** ** ** ** ** ** ** ** ** * * ***** *** * /= 10;
* * ** *** ** * ***
** *** * ** * ** * count;
}

int main(void)
{
* * * ** * **** * **** * n;
*** ** * **** ** ***** * ** *** * &n);
* ** *** * ***** ** *** * * * has %d * * **** * *** ** *
* * ** * * * ******** 0;
}
trả lời bởi (-168 điểm)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 thích 0 k thích
Hidden content!
#include <stdio.h>
int num_digits(int n)
{
** * * ** *** **** cnt=0;
* ***** * * * * * * **** * != 0)
* **** * * *** * * *
** **** ******** ** ** ** ** ** * ** * * * *** * /= 10;
** ** *** ** * ** *** * ** * * * * * * * * *
***** * * ** * *** *** * **
*** * **** * * cnt;
***** ** * * ***** * *

int main(void)
{
*** ** * * * ** * * ** n;
** * *** * ** * * * **** ** * &n);
** * *** ** * ** * * **** * has %d **** ** * * *
* *** *** ** * *** * ** *** 0;
}
trả lời bởi (323 điểm)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 thích 0 k thích
Hidden content!
#include <stdio.h>
int num_digits(int n)
{
* * * * ** * *** **** ** count=0;
**** ************* *
** *** * **** **** ** *** != 0)
* ** *** * *
* * ***** *** ** *** *** ** *** **** ** ****
* ** * * ** ** * * ** ** *** * * * *** * * ++;
* * * *** * ** * ** * * **
** ** * * ** ** *** *
* ** * * * * ** ** * * ** count;
}
int main(void)
{
*** *** * * ***** ** n;
* * * *** ** *** ** * * * * ** * &n);
* ***** *** * ** ** * ** *** * *** *** has %d **** ** * ***
* * ** * * ** * **** ** * 0;
}
trả lời bởi (-193 điểm)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 thích 0 k thích
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;
}
trả lời bởi (-167 điểm)
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 thích 0 k thích
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;
}
trả lời bởi (-167 điểm)
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:172.70.100.104
©2016-2025

Những câu hỏi liên quan

0 thích 0 k thích
47 trả lời
[Exercise] Coding (C) - đã hỏi ngày 7 tháng 12 năm 2017 trong Chapter 9: Functions bởi semicolon (5.2k điểm)
ID: 35785 - Xem được từ: 2017-12-07 18:00 - Hiệu lực đến: Không giới hạn
| 9.7k đã xem
0 thích 0 k thích
79 trả lời
[Exercise] Coding (C) - đã hỏi ngày 28 tháng 12 năm 2017 trong Chapter 9: Functions
ID: 39986 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 19k đã xem
0 thích 0 k thích
70 trả lời
[Exercise] Coding (C) - đã hỏi ngày 7 tháng 12 năm 2017 trong Chapter 9: Functions bởi semicolon (5.2k điểm)
ID: 35783 - Xem được từ: 2017-12-07 18:00 - Hiệu lực đến: Không giới hạn
| 14k đã xem
0 thích 0 k thích
50 trả lời
[Exercise] Coding (C) - đã hỏi ngày 13 tháng 12 năm 2017 trong Chapter 9: Functions bởi semicolon (5.2k điểm)
ID: 37278 - Xem được từ: 2017-12-14 18:00 - Hiệu lực đến: Không giới hạn
| 12.2k đã xem
12,783 câu hỏi
183,442 trả lời
172,219 bình luận
4,824 thành viên