{ }codelper
do-while loopcountingP0052

Count the digits

The first line gives the count n, and the second line gives n integers separated by spaces.
For each number print how many decimal digits it has, one per line.
The minus sign is not a digit, and 0 has one digit.
For example 0 → 1, -42 → 2, 1000 → 4.

Input
1 ≤ n ≤ 1,000, -1,000,000,000,000,000 ≤ number ≤ 1,000,000,000,000,000 (±10^15)
Output
n lines, each a digit count
Example 1 · stdin
6
0 7 -42 100 -1000 999
stdout
1
1
2
3
4
3
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

A divide-by-10 counting loop must count first and test after, so that 0 counts as one digit: do { count++; x /= 10; } while (x != 0);.
Testing first, as in while (x != 0), never runs for 0 and gives zero digits.
Languages with do-while: C, C++, Java, C#, JS, TS.
Python and Craykon have none, so build 'at least once' with while True: … if x == 0: break (Craykon while (true) … break;).
The negative-number trap: integer / in C, C++, Java and C#, and Craykon Mathf.Quot, truncate toward zero (-42 → -4 → 0), so negatives reach 0 on their own, but Python // floors, so -5 // 10 == -1 and -1 // 10 == -1 forever — take abs(x) first.
In JS/TS Math.floor(x / 10) has the same problem, so use Math.trunc.
Beware the shortcuts too: string length (str(x), String(x).length) counts the '-', and floor(log10(x)) + 1 fails for 0, and for 999,999,999,999,999 log10 rounds to 15.0 and gives 16.
Values exceed 2.1 billion, so C, C++, Java and C# read them as long (long long).

Pythonmain.py · 11 lines
n = int(input())for x in map(int, input().split()):    x = abs(x)  # 파이썬 // 는 내림이라 -5 // 10 == -1: 음수는 0에 닿지 않는다    count = 0    # 파이썬에는 do-while이 없다: while True + break로 "적어도 한 번"    while True:        count += 1        x //= 10        if x == 0:            break    print(count)
JavaScriptmain.js · 11 lines
const [n, ...xs] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out = xs.map((x) => {  let count = 0;  // do-while: 0도 한 자리로 센다(몸통이 적어도 한 번 돈다)  do {    count++;    x = Math.trunc(x / 10); // Math.floor는 음수에서 -1에 멈춘다  } while (x !== 0);  return count;});console.log(out.join("\n"));
TypeScriptmain.ts · 12 lines
import { readFileSync } from "node:fs";const [n, ...xs]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out: number[] = xs.map((x) => {  let count = 0;  // do-while: 0도 한 자리로 센다(몸통이 적어도 한 번 돈다)  do {    count++;    x = Math.trunc(x / 10); // Math.floor는 음수에서 -1에 멈춘다  } while (x !== 0);  return count;});console.log(out.join("\n"));
JavaMain.java · 23 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(br.readLine().trim());        StringTokenizer st = new StringTokenizer(br.readLine());        StringBuilder out = new StringBuilder();        for (int i = 0; i < n; i++) {            long x = Long.parseLong(st.nextToken());            int count = 0;            // do-while: 0도 한 자리로 센다. / 는 0 쪽으로 버려서 음수도 0에 닿는다            do {                count++;                x /= 10;            } while (x != 0);            out.append(count).append('\n');        }        System.out.print(out);    }}
C#Main.cs · 21 lines
using System; class Program{    static void Main()    {        Console.ReadLine();        foreach (string s in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries))        {            long x = long.Parse(s);            int count = 0;            // do-while: 0도 한 자리로 센다. / 는 0 쪽으로 버려서 음수도 0에 닿는다            do            {                count++;                x /= 10;            } while (x != 0);            Console.WriteLine(count);        }    }}
C++main.cpp · 19 lines
#include <iostream> int main() {    std::ios::sync_with_stdio(false);    std::cin.tie(nullptr);    int n;    long long x;    std::cin >> n;    for (int i = 0; i < n; i++) {        std::cin >> x;        int count = 0;        // do-while: 0도 한 자리로 센다. / 는 0 쪽으로 버려서 음수도 0에 닿는다        do {            count++;            x /= 10;        } while (x != 0);        std::cout << count << '\n';    }}
Cmain.c · 18 lines
#include <stdio.h> int main(void) {    int n;    long long x;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        scanf("%lld", &x);        int count = 0;        /* do-while: 0도 한 자리로 센다. / 는 0 쪽으로 버려서 음수도 0에 닿는다 */        do {            count++;            x /= 10;        } while (x != 0);        printf("%d\n", count);    }    return 0;}
Craykonmain.cray · 17 lines
n = Console.ReadLine().ToNumber();수들 = Console.ReadNumbers();foreach (x in 수들){    개수 = 0;    // 크레이콘에는 do-while이 없다: 동안 (참) + 그만으로 "적어도 한 번"    while (true)    {        개수++;        x = Mathf.Quot(x, 10); // 0 쪽으로 버려서 음수도 0에 닿는다        if (x == 0)        {            break;        }    }    Console.WriteLine(개수);}