Hello There, Guest! Login Register
Advertise Here | Remove Ads

C Program to Count Number of Digits of an Integer

  • 0 Vote(s) - 0 Average
Thread Closed


02-03-2016, 07:09 PM #1
Sadness Offline MODERATOR MODERATORS
Posts:4,001 Threads:565 Joined:Sep 2014 Reputation: 13 Sex Female
FFUC: $0
Country: Bangladesh
Code:
#include <stdio.h>
int main()
{
  int n,count=0;
  printf("Enter an integer: ");
  scanf("%d", &n);
  while(n!=0)
  {
      n/=10;             /* n=n/10 */
      ++count;
  }
  printf("Number of digits: %d",count);
}

[Image: SeoClerks728x90.png]

03-02-2016, 02:51 AM #2
gkiz Offline FFU NEWCOMER FFU NEWCOMER
Posts:28 Threads:9 Joined:Sep 2014 Reputation: 0 Sex Undisclosed
FFUC: $0
thank you very much for this source code. I am a beginner of C and I have tried much to do this but could not so. Big Grin

04-01-2016, 01:37 PM #3
WHUK Offline FFU STARTER FFU STARTER
Posts:53 Threads:12 Joined:Mar 2016 Reputation: 0 Sex Male
FFUC: $0
Country: United Kingdom
Another method to count the total number of digit using for loop

#include<stdio.h>
int main(){
  int num,count=0;

  printf("Enter a number: ");
  scanf("%d",&num);

  for(;num!=0;num=num/10)
      count++;

  printf("Total digits is:  %d",count);

  return 0;
}

Sample output:
Enter a number: 8965
Total digits are:  4






Forum Jump:


Users browsing this thread:1 Guest(s)

Advertise Here | Remove Ads