Back In the print window choose "Save as PDF" as the destination
NOTE.CS

Length (count digits) of a number

1 topic · 28 Nov 2026
1

Length (count digits) of a number

Problem Solving
Problem: Count the digits of a number (1234 -> 4).

A do-while loop is used so that the number 0 is counted as 1 digit.

Solution 1: do-while loop · C++
int main() {
    int num = 1234;
    int len = 0;
    do{
        num = num / 10;
        len++;
    }while(num != 0);
    cout << len;   // 4
}
Solution 2: String method · C++
int num = 1234;
int len = to_string(num).length();
cout << len;