Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following is a C function thatcomputes the number of (decimal) digits in an

ID: 3616439 • Letter: T

Question

The following is a C function thatcomputes the number of (decimal) digits in an integer: int numdigits(int x) { int t =x ,n=1;    while(t>=10)     { n ++;        t = t/10;      }    return; } It does not work for negative number. Could you pleasere-write so it will? I would like to have the full code including main so I couldtest it in Eclipse. thanks, Adam PS I rate 10 The following is a C function thatcomputes the number of (decimal) digits in an integer: int numdigits(int x) { int t =x ,n=1;    while(t>=10)     { n ++;        t = t/10;      }    return; } It does not work for negative number. Could you pleasere-write so it will? I would like to have the full code including main so I couldtest it in Eclipse. thanks, Adam PS I rate 10 It does not work for negative number. Could you pleasere-write so it will? I would like to have the full code including main so I couldtest it in Eclipse. thanks, Adam PS I rate 10

Explanation / Answer

#include using namespace std; int numpositivedigits(int x) { int t =x , n=1;     while(t>=10)     { n ++ ;        t = t/10;       }     return n; } int numnegativedigits(int x) {      int t = -1 *x , n=1;     while(t>=10)     { n ++ ;        t = t/10;       }     return n; } int main() { int number;     printf("enter a number");;     scanf("%d", &number);         if (number >=0){                   printf("%d",numpositivedigits(number));                   }                        else     {                   printf("%d",numnegativedigits(number)); }         printf(" ");     system("pause"); }