Q:

C Program to Count Number of Digits of a Number (Integer)

belongs to collection: Loops C Programs for Practice

0

Write A C Program to Count Number of Digits of a Number (Integer) Using While Loop.

 

Logic:

There are two methods for counting a number of digit in a number 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<stdio.h>
#include<math.h>
int main()
{
   int n,count=0;
  
   printf("Enter Any Number To Count Digit : \n\n");
   scanf("%d", &n);
  
   count=log10(n)+1;
   
   printf("\nNumber of Digits Is = %d",count);
 return 0;
}

 

Output:

Enter Any Number To Count Digit :

12345

Number Of Digit Is =5

need an explanation for this answer? contact us directly to get an explanation for this answer
need an explanation for this answer? contact us directly to get an explanation for this answer

#include<stdio.h>
int main()
{
   int n,count=0;
  
   printf("Enter Any Number To Count Digit : \n\n");
   scanf("%d", &n);
  
   while(n!=0)
   {
       n/=10;          
       ++count;
   }
   printf("\nNumber of Digits Is = %d",count);
 return 0;
}

 

Output:

Enter Any Number To Count Digit:

123456789

Number Of Digit Is =9

EE

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (2)

C Program For Find A Generic Root Of Number Using ... >>
<< C Program to Check a Number is Palindrome or Not U...