Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Count number of digits in integer

#include <stdio.h>
int main() {
  long long num;
  int count = 0;
  printf("Enter an integer: ");
  scanf("%lld", &num);
 
  // iterate at least once, then until n becomes 0
  // remove last digit from n in each iteration
  // increase count by 1 in each iteration
  do {
    num /= 10;
    ++count;
  } while (num != 0);

  printf("Number of digits: %d", count);
}

    Leave a Reply

    Your email address will not be published.

    Need Help?