-->
Showing posts with label count number of digits present in a number.. Show all posts
Showing posts with label count number of digits present in a number.. Show all posts

program to count number of digits present in a number.

//program to count number of digits present in a number.
#include<stdio.h>
#include<conio.h>
void main()
{
   int n,rem,count=0;
   printf("enter a number for 'n'\n");
  scanf("%d",&n);
  while(n!=0)
    {
        rem=n%10;
       count++;
        n=n/10;    
   }
printf("total digits =%d",count);
getch();
}
--------------------------------------------------------------------------------------------------
logics in mind:-
>first we enter a number(for 'n').
->If we have number 123 then it has 3 digits and they are 3, 2 and 1 or 1,2 and 3. It means, first we have to get 3 then 2 and then 1 and we have to count them.        
                  ->For this, 
                 ->we have to divide by 10 to get last digit as remainder 
                 ->we also use 'count' to start counting  digits.
                -> to get second digit, we get first 12 and for this , we use 123/10.It is done to get integer                           part  only.
->We repeat this until the value reaches 0. We use loop for this as shown above in the code.     
->at last we display total number of digits stored in variable 'count'.