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

寫一個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 | 3.8k views

45 Answers

0 like 0 dislike
Hidden content!
#include *** ** ** **
int * * ** * n)
{
* * * **** ** * YOUR CODE HERE*/


* ** *** * * * ** * num_digits = 0;
** ** ** *** ** ** *** > 0)
** * * * * **** * *
** ** * * * ** *** * ** * *** ** ** ** * ** ***** ***
*** * ** * *** *** * * * ** *** * ** ** **

** * ******** *** * *


* ****** *** ** * **** * * num_digits;

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

int main(void)
{
** * * * * ** ** n;
* ***** * ***** ** * * ** * &n);
* * ** * *** *** ***** ** * * has %d * * * *** * * * *
* * ***** *** *** 0;
}
answered by (-304 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=1,j;
** * *** * (n>=10){
*** ** * ** *** ****** **** * * *** * *** * *
* * ** * ** * ** * * ** ** ** *******
* * * *** ** * ** * ** **
***** ** ** *** * * * ******* * * j;
* * ** * *
** ** *** * *
* * * * * * ** * ** ** * ** 1;
*** *** * * **** * *
}


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

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

** ** **** ****** * * * i;
* * * * * ** *** ** *
* ******* **** * * *** * *
**** * * * *** *** * * ******* **** * ** **
*** ** ** * *
* * ** * * ** ** * * i;
****** * * * ** ** ****** *
* ** ** ** *** **** OF YOUR CODE*/
}

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

**** ** *** * * ********** * != 0)
** * ** * * * **
*** * ***** ** * * *** *** * *** * * * /= 10;

*** * ** * * ** ** * **** * * * * ** *
**** ** * ***** ** **

* * ** ** ** ** * * ** * count;
}

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

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

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

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

** ** * * ** **** ** count = 0;

* ** * * **** ** * * ***** * *** != 0)
** **** ** ** * ** ** *
***** * * * * ***** **** **** * * * * * /= 10;

** * * * * * ***** * * * ** *** **** ** * ** *

***** ** ** * * **** * * * ** ** * ** * ***** * * < 10)
* * *** **** ** * *** * * ** *** * * * * ** **
* ********* * ** * ** **** * ** ** * * * ** **** * ** ** * ***** count - 1;
* * * ******* * ***** * ** * * * * * ** * ** *****
* * ***** **** * *
}

int main(void)
{
   int n;
* * ** * * * * **** * * ** &n);
* * * * * * *** * * has %d * *** ** ** *** *
* ** * ** * ** ***** 0;
}
answered by (-285 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong 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.7.165
©2016-2024

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
| 3.5k 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
| 7.4k 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
| 5.1k 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
| 4.5k views
12,783 questions
183,443 answers
172,219 comments
4,824 users