Monday, May 27, 2019

Write a C Program to Count Number of Digits in an Integer

Write a C Program to Count Number of Digits in an Integer

Program:

#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: 56789
Number of digits: 5
--------------------------------
Process exited after 5.369 seconds with return value 0
Press any key to continue . . .

0 Comments:

Post a Comment