0 like 0 dislike
5.5k 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 | 5.5k views

45 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
int num_digits(int num)
{
* * *** * * * * * **** i;
* * * ** * ** ** * * **
*** * * * * * ** * * * *
*** * * *** *** * ** **
***** ***** *** * * *** **** * *** ** ***** ** * *** * * ** *** ***
* ** ** ***** ***** * ** ** * * * * * * * *** ** ** * * * * ***
** * * **** *** * ***
* **** ***** ** **** i;

*** ** * ******** * YOUR CODE HERE*/

* **** *** *** ****
* ** ** * ** * ** OF YOUR CODE*/
}

int main(void)
{
   int 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)
{
* * * ** * * * * digits=0;
** * * * * * * * ****
**** * *******
* ** ** ** * ** ** **** * *** * * * * * *
** ** ** ** * *** ******** *** * * *** * **
** * * ** * **
* * **** ** * * * digits;
}

int main(void)
{
* * ** **** * ** ** n;
* * ** * * *** * ** * *** * * * &n);
** **** ******* *** * * ** has %d * *** ** ** ** * **** **
** * * ** *** * 0;
}
answered by (-254 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 * * *** ***
int * * n)
{
** * ** ** ******** ** *** YOUR CODE HERE*/
* * **   int c;
***** * ** * *******
* *** *** * * * * ***
* *** *** ** ** ** ** *   n=n/10;
** * * ** *** * * *** **   c++;
** ** * * **** *
** ***** * **** **
** * * ** ** ** ** *** c+1;
*** * ** *****
** * * * * * *
**** ** ** * OF YOUR CODE*/
}

int main(void)
{
* * **** * * * * ** ** n;
******** *** * * * * ** * **** * ** *
* * * * * * * * ** * * has %d **** ** * ** ** *
*** * * * ***** *** 0;
}
answered by (-249 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)
{
** * * *** ** ** *** ** YOUR CODE HERE*/
** ** * ** ** count = 0;
* ** ** ****** * ***** ** != 0)
** * * ** ***
* *** * * * ** * **** ** * ** ** * * *** * * n = n/10
** ****** *** *** * * ** ** ** * ** * * * **** /= 10;
** ** *** * * *** ** * * * * * ** * *
**** * ** * * ** * ***
*** * * ** * ** * * ******* count;
** * ** ** **** * * * OF YOUR CODE*/
}

int main(void)
{
* ***** * *** *** * * * * * n;
* * * *** ** * * *** * * * * *** * &n);
* **** ** * * * * ** ** has %d ** **** * * **
* * * * *** * * * * ** * 0;
}
answered by (-233 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 * * * *** * *
int num_digits(int n)
{
* ***** ** digit=1;
**** *** ** ** * ** ***
** * ** ** *** ** ***** * *** *
***** ***** * * * ** *** * *** * * *

**** *** *** ** ** digit;

}

int main(void)
{
** * ** ***** *** *** n;
* * * ** * *** * * **** * &n);
* * * * ** ** * has %d ** **** ***** **
* * ** * * * * * 0;
}
answered by (-16 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 * * *** *
int ******* n)
{
** ** i=0;
** **
 {
* * * * ** ** ** * * * **
* * ** * ** **** * *** *** *****
** * ***
** * *** i;
}

int main(void)
{
** **** ** * ** n;
*** ** * * * * * ** * ** * * * &n);
* ***** ** ** * * ****** has %d *** ****** ******* *
* **** ** ******* **** 0;
}
answered by (-284 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 ** ******
int ****** **** n)
{
* ** ** i=0;
* ** * * * **
 {
* * ***** * ****** ** ** *** *
** * **** *** *** * ** ** * * *
*
* ** *** i;
}

int main(void)
{
**** ** ****** ** n;
* * ******* ** *** * * * ** * &n);
* ** * *** ****** ** * **** has %d * ** ** ** ** ** * *
** ** ****** * * * ** **** 0;
}
answered by (-258 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 ** *** ***** *
int * *** n)
{
** * * ** * ** * * **** YOUR CODE HERE*/

** * * ** * * ** ** *** ** ** *
** * ****** ** *** OF YOUR CODE*/
}

int **
{
** * **** * * *** * n;
* * * **** * * ** *** * *** * ** * *
*** ******** * * ** * has %d * ** ***** * * * * ** *
** * ** * * ** * * 0;
}
answered by (-258 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int num_digits(int n)
{
** ** *** * * * * digits=0;
** * * * * *** * *
* * ****** ** **
* * * * ** * * * * * * **** *** *
* ** * ** * **** * * * * ** * * * * * ** * *
*** ** **** *** *
** ** ** ** ** ** * digits;
}

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 * **** * * *
int num_digits(int n)
{
* **** ** * ** a = 0;
** ** ** * * (n>0)
** * ** * * *
* ** * ** * * ** * * * * * ** ** *
** * *** * ******* *** **** * * ** * *
* ***** *** * *
* ** ** ****** * a;
}

int main(void)
{
*** ***** *** ** n;
* ******* *** * ** ** &n);
** * ** ** *** ** * has %d * * * * * * ** * * *
** ** * ** ** *** ** 0;
}
answered by (-108 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.17.10
©2016-2025

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
| 4.9k 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
| 10.2k 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
| 7.3k 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
| 6.3k views
12,783 questions
183,442 answers
172,219 comments
4,824 users