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

45 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
int num_digits(int n)
{
** ** * *** * ** * * YOUR CODE HERE*/

*** * ** * ** * * ** ** * * digits=0,i,neg=0;
* *** * * * *** * * * * ** long int pow=1;
* ** * ** ** * * * ** (n<0){
* * * ** ** * ** * ** * ** * ** ** **** ** ***
* * * * ** ***** * * * ** ** * ***** **
*** **** * ** ** ********
* **** ** ** * ** ** * ** * **
* * * **** *** ****** * ****** ** * ****** *** * ** *** ****
* * * ** * ***** * ** ****** *** * ** * * * **
* * * * ** ** ** ** ******* * * * **** ** *
* ***** * * * * *** ** **
* * ** **** *** ** *** (n==0){digits++;}
*** ** * ** * ** *** (neg==1){n=n*-1;}
* ** * ** * ** ** * ** * digits;
* ** ***** *** * * * **** ** ** ***
* *** * * ** * *** OF YOUR CODE*/
}

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

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

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

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

int main(void)
{
* ** ** * * ** n;
** * * ** * ** * ** * &n);
* *** *** **** * *
** ** ********* ** ** **** has %d *** *** * * * *
* **** ** *** * *
******* **** *** * ** *** 0;
}
answered by (1.1k 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.70.178.29
©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
| 6.6k 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
| 13.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
| 10k 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
| 8.4k views
12,783 questions
183,442 answers
172,219 comments
4,824 users