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 10Explanation / 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"); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.