Number 1818 1818 = 1+8+1+8 = 36 step 1 (2 digits output) 36 = 3 + 6 = 9 step 2 (single digit output) result: 9 (final count of the digits ) #### use strict; use warnings; my $total1; for my $num (1818..1818){ my $total1 = &spl($num); print "$num == 9\n" if ($total1 == 9); } ################# not working as i expected ####### case 1 sub spl{ my ($num1) = @_; my $total = 0; $total = $total + $_ for (split '', $num1); print "total: $total\n"; my $len = length ($total); print "length: $len\n"; &spl($total) if ($len != 1); print "final: $total\n"; return $total; } ########## working perfectly ######### case 2 #sub spl{ # #my ($num1) =@_; # #my $total = 0; # #$total = $total + $_ for (split '', $num1); # #my $len = length ($total); # #return $total if ($len == 1); # #&spl($total) if ($len != 1); # #} output: ------- total: 18 length: 2 total: 9 length: 1 final: 9 final: 18 Expected output: ---------------- total: 18 length: 2 total: 9 length: 1 final: 9 1818 == 9