Total digits of integer value
Write a program to find the total digits of a integer value without converting them into string
int a = 3421111
output: 7
- <?php
-
- $go_running = true;
- $no_of_digits = 0;
- $nines = 0;
- //make integer to positive
- if ($int_val < 0) {
- $int_val = -($int_val);
- }
- while ($go_running == TRUE) {
- $nines = (int) ($nines );
- if ($int_val > $nines || $int_val == 0) {
- $int_val = ($int_val == 0) ? 1 : $int_val;
- $no_of_digits++;
- $nines = $nines . '9';
- } else {
- $go_running = FALSE;
- }
- }
- return $no_of_digits;
- }
-
- echo total_digit_of_integer(-10);
- ?>