Count Digits
PHP
Medium
4 views
Problem Description
Print how many digits are in n (for n=0, answer is 1).
Input Format
One integer n.
Output Format
One integer count.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$n=intval($inputText);
if($n<0) $n=-$n;
if($n===0){ echo 1; exit; }
$c=0;
while($n>0){ $c++; $n=intdiv($n,10); }
echo $c;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!