Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi Monks,

Today I wrote a small script for my friend to check the sum of the digits recursively (till single digit of length), numbers range from 1500 to 2300. For example:

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 )

I wrote the below code and found the final total is printing continuously. I expected only 9 but it is printing both 9 and 18 continuously. I solved the issue later (case 2). But I want to know the reason why it is printing continuously 9 and 18. Because of that I am not getting final output '1818 == 9' in the main program.

I 'super searched' in perlmonks and found following links. subroutine recursion question,Recursion problem, Recursion. But I was not able to find correct answer. Even I googled but I didn't get correct answer. Even I flushed using $| but not able to get expected output. Where am I going wrong? Could someone explain where I am making mistake?

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

Thanks in advance

Prasad

updated: added the line, 'Because of that I am not getting final output '1818 == 9' in the main program.'


In reply to Question on Recursion by prasadbabu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-25 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found