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

45 Answers

0 like 0 dislike
Hidden content!
#include * ** ** * * ***
int num_digits(int n)
{
* * * ** digit=0;
* * * *** *** * **
*** ** **
** **** ** ***** * *** * * * ** /=10;
* * * * * * * * ***** ** * * *
**** * ** *** ** * ** ****
* *** ** ** * *** digit;
}

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

int main(void)
{
** * * * * ** * * n;
* ** * **** * ** ** * *** &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 ** * *
int num_digits(int n)
{
* *** * * * count=0;
* * *** * * *
* * ** *
** * **** * * *** **** **** * *
** ** * ** ** ********
** * ***
** *** ** * ** count;
}

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 <stdio.h>
#include <stdlib.h>

int main()
{
* * *** ** * ** ** * a;
* ** ** * * * * * *** sum=0;
** * ** ** * * ***** i=0;
* * * ** ** * * * * ** * * * * ** * *
** * * ** * ***** **** * ** (a!=0)
* ** ***** * * *
** * * * * **** ** **** * * ***** ** ***** += a%10;
*** ***** *** * * ** * * *** *** * * *
* ** * * * * * * * ********** ** ** *
**** * * * * ***
* * ** ** * **** * ** **** has %d digit(s)",a,sum);
*** * ***** * ** ****** ** * ** 0;
}
answered by (-127 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

int main()
{
*** *** * * *** * a;
* **** * **** ** ** sum=0;
** * ** * * ***** ** i=0;
* ***** * * *** * **** * * * * ***
*** * ** * * ** *** * * *** *** (a!=0)
* *** *** * * ** **
*** * *** * * *** *** * * ** * ** *** **** * ** **** * += a%10;
** ** *** * ** *** * *** * ***** ** ******* * * * **
** *** * * * * * *** * * * * ** ** **
* ***** ** * ***** * **
* ** * *** *** ***** * * * *** %d",i,sum);
** ** * *** *** ** * * * 0;
}
answered by (-127 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int num_digits(int n)
{
*** * * * * * * i=0;
* ** ** * * ** ** ** * ***
* *** ** * *
* ** ** ** *** ** * ** * * *
*** ** * ** ** * ** * * ** ** * * * ***
* * ******** *
* *** * ** * ** *
}

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,c=0;
* * *** * * * ** ** * **** ** * * ***
* *** ** * *** *** ** * *** * ** *********
* ** ** * * ** *** * * ***** * ** ** **** **** * *** **** ** ** * *
*** * * ******* ****** ** **** ***** * * * * * *** ** * **** **
* ***** * *** *** ** *** ** *** ** ***** ** * * * * **** ** * ** ** ** ** *** * * ** * *
* *** * *** ** ******* * * ****
*** ** ** * ** ****** ** * * * * * *** * * *
** * * * * *** ** * * * * **** * ** ** ** **** * * * **
** * ** ** * * ** ** * **** * ** ** ****** *** * * ** *** ** * *
*** ***** * *** * *** * ***** * ** * * ** *** * *
    }

* ** * ** *** * * ** c;
}

int main(void)
{
   int n;
*** * * * * * * *** **** &n);
** ** * **** ****** *** ** has %d digit(s)",n,num_digits(n));
   return 0;
}
answered by (54 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,c=0;
* ** ** * * *** *** ** * * * ****
* * ** * * ** ** ** ** ** * * * * ** ***** ** ** *** *
*** **** * * *** * *** * **** ******** * *** * *** *
* ** ** ** * ** ** **** *** *** * ***** * * * * *****
** * * ** * ** *** * ** *** * * * ** ** * ** * ** ** * ** ** ** *** **** * * * ** ** **
* ** * * ***** * ** * * **** * ** *
***** ** * ** * ** *** * * * *** * * ***** *
** ** * * **** * *** * * ** *** * **** * * * ** ***** * ** * ** * **** **
* *** ** * * * ** * * ** ** ** * ** * ******
*** ** * * * ***

**** * * * * * ** ** c;
}

int main(void)
{
   int n;
* ** *** * * *** **** ** ****** ** &n);
* ** * **** * ** ** has %d digit(s)",n,num_digits(n));
   return 0;
}
answered by (54 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 0
#include <stdio.h>
int num_digits(int n)
{
    int i,c=0;
    for(i=1;i<100;i++){
        if(n/10!=0){
            n=n/10;
            c++;
            //printf("%d\n",n);
        }
        else{
            c++;
            break;
        }
    }

    return c;
}

int main(void)
{
   int n;
   scanf("%d", &n);
   printf("%d has %d digit(s)",n,num_digits(n));
   return 0;
}
0 like 0 dislike
Hidden content!
#include <stdio.h>
int num_digits(int n)
{
   int a;
** * * *** **** * **** **
* * * * ** * ** ** *** *
** *** *** * ** ***** * *** * **** * ** ** ** * * ** **** **
* *** * * * **** * *** ***** * * *** ***** ****** * * * * ***
** ** **** ** * *** ** ** * * * ** ** * ** ** * * *** * * **** * a;
* * ***** * * *** * ** ** * ****
}

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


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

int *** *
{
** ** * ** * * n;
* * *** * ** * **** **** * **** **
* ** ** * ** * * ** **** has %d * ** ** * ** *
***** *** * * * ** 0;
}
answered by (-336 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.214.205
©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
| 8k 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
| 16.1k 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
| 12.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
| 10.2k views
12,783 questions
183,442 answers
172,219 comments
4,824 users