Count Digits in a Number
Count how many digits a number has.
Sample input
12345
Sample output
5
Solution
n = 12345
count = 0
while n > 0:
count += 1
n //= 10
print(count)
let n = 12345, count = 0;
while (n > 0) {
count++;
n = Math.floor(n / 10);
}
console.log(count);
public class Main {
public static void main(String[] args) {
int n = 12345, count = 0;
while (n > 0) {
count++;
n /= 10;
}
System.out.println(count);
}
}
fun main() {
var n = 12345
var count = 0
while (n > 0) {
count++
n /= 10
}
println(count)
}
var n = 12345
var count = 0
while n > 0 {
count += 1
n /= 10
}
print(count)
void main() {
int n = 12345, count = 0;
while (n > 0) {
count++;
n ~/= 10;
}
print(count);
}
#include <iostream>
using namespace std;
int main() {
int n = 12345, count = 0;
while (n > 0) {
count++;
n /= 10;
}
cout << count << endl;
return 0;
}
#include <stdio.h>
int main() {
int n = 12345, count = 0;
while (n > 0) {
count++;
n /= 10;
}
printf("%d\n", count);
return 0;
}