Monday, 7 March 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 method 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)

No comments:
Write comments

Recommended Posts × +