for ($count = 2; $count <=sprintf($number/2); $count++) { $result = $number / $count; { <-------------------------------- Extra Block.. push (@factor, $result); } <-------------------------------- Extra Block.. }
There's no need for the extra block embracing push(@factor, $result)...

While you can use sprintf, printf or even int to chop away the decimal part the array @factor can end up having repeated elements and that doesn't sound efficient..exercise on some workaround and we'll be here if you needed assistance . :)

Make it a habit to always declare your variables and turn strictures on at the top of your program that can save you from many horrendous evil bugs that can become hard to track otherwise...

use strict; use warnings; print "Enter a number you want to factor: \n"; my $number = <STDIN>; my @factor; for (my $count = 2; $count <=sprintf($number/2); $count++) { my $result = int($number / $count); #other approaches were shown.. push (@factor, $result); } print "The factors of $number are: \n"; foreach (@factor) { print "$_\n"; }


Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

In reply to Re: how to make perl only print whole numbers by biohisham
in thread how to make perl only print whole numbers by derpp

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.