C Program

Object:-Write a Program to Count Number of Digits in an Integer

#include <stdio.h>
int main()
{
 long long n;
 int count = 0;
 printf("Enter an integer: ");
 scanf("%lld", &n);
 while(n != 0)
 {
  // n = n/10
  n /= 10;
  ++count;
 }
 printf("Number of digits: %d", count);
}

Output

Enter an integer: 352
Number of digits: 3
 

Explanation

  • The integer entered by the user is stored in variable n. Then the while loop is iterated until the test expression n != 0 is evaluated to 0 (false).







You may also like: