07/03/2016

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

Problem:- 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 


Method 1:- Using inbuilt Function

Example :- Number = 12345

Number_Of_Digit = log10(Number)+1

Number_Of_Digit = 5

Method 2:- Using Loop


while(n!=0)
{
  n/=10;          
      ++count;
}

Try Yourself C Program For Find A Grade Of Given Marks Using Switch Case

Solution:-

Method 1:-

#include<stdio.h>
int main()
{
  //Ghanendra Yadav
  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;
}

Method 2:-



#include<stdio.h>
#include<math.h>
int main()
{
  //Ghanendra Yadav
  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:-

Method 1:-

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

Method 2:-

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

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: