There's no need for the extra block embracing push(@factor, $result)...for ($count = 2; $count <=sprintf($number/2); $count++) { $result = $number / $count; { <-------------------------------- Extra Block.. push (@factor, $result); } <-------------------------------- Extra Block.. }
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"; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |