Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>  
int main()  
{  
   int n;  
   int count=0;  
   scanf("%d",&n);  
   while(n!=0)  
   {  
       n=n/10;  
       count++;  
   }  
     
   printf("The num has %d digits" ,count);  
    return 0;  
}  


if works for all numbers but if i type num 0 it doesnt print that it has 1 digit , it prints 0 . How can i correct that ?

What I have tried:

................................
Posted
Updated 18-Nov-22 23:38pm

1 solution

Simplest solution? change your initial value, and your test.

If you start with a count of one, and test for "greater than or equal to ten" as your loop check, it'll give you the right answer for all positive values.
 
Share this answer
 
Comments
CPallini 19-Nov-22 5:42am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900