Dremendo Tag Line

Input a number and count the number of digits in it using while loop in C++

while Loop - Question 11

In this question, we will see how to input a number and count the number of digits in it in C++ programming using while loop. To know more about while loop click on the while loop lesson.

Q11) Write a program in C++ to input a number and count the number of digits in it using while loop.

Program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int n,c=0;

    cout<<"Enter any number ";
    cin>>n;

    while(n>0)
    {
        c=c+1;
        n=n/10;
    }
    cout<<"Total Number of Digits = "<<c;
    return 0;
}

Output

Enter any number 42638
Total Number of Digits = 5
video-poster