C++ Program to Count the Number of Digits in a Given Number

In this program, the user is ask to enter the number, then the number of digits the given number have will be displayed on console.



Source Code
#include<iostream>
using namespace std;


int main()
{
    int num, digit=0;
    //;
    cout<<"Enter a number: ";
    cin>>num;

    while(num!=0)
    {
        digit++;
        num = num / 10;
    }

    cout<<"No. of digits = " << digit;
    return 0;
}
Output
Enter a number: 42513
No. of digits = 5

Enter a number: 47
No. of digits = 2