Home: Perl Programming Help: Beginner:
A small doubt on while loop



smidhunraj
New User

Sep 5, 2013, 5:30 AM


Views: 3387
A small doubt on while loop


Code
#!usr/bin/perl 
print "Enter the number";
$n=<>;
$t=$n;
$d=0;
while ($n > 0)
{
$d=$n%10;
$n=$n/10;
print "$d";
}
exit;


The output of this program is

Code
Enter the number123 
32100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Why i am i getting these many number of zerosTongue


FishMonger
Veteran / Moderator

Sep 5, 2013, 6:34 AM


Views: 3381
Re: [smidhunraj] A small doubt on while loop

If you output $n, the answer should become clear.


smidhunraj
New User

Sep 5, 2013, 7:40 AM


Views: 3377
Re: [FishMonger] A small doubt on while loop

now i changed my program to

Code
#!usr/bin/perl 
print "Enter the number";
$n=<>;
$t=$n;
$d=0;
while ($n > 0)
{
$d=$n%10;
$n=$n/10;
print "$n";
}
exit;

i get the out put as

Code
Enter the number

entered as 123

Code
12.31.230.1230.01230.001230.0001231.23e-051.23e-061.23e-071.23e-081.23e-091.23e-101.23e-111.23e-121.23e-131.23e-141.23e-151.23e-161.23e-171.23e-181.23e-191.23e-201.23e-211.23e-221.23e-231.23e-241.23e-251.23e-261.23e-271.23e-281.23e-291.23e-301.23e-311.23e-321.23e-331.23e-341.23e-351.23e-361.23e-371.23e-381.23e-391.23e-401.23e-411.23e-421.23e-431.23e-441.23e-451.23e-461.23e-471.23e-481.23e-491.23e-501.23e-511.23e-521.23e-531.23e-541.23e-551.23e-561.23e-571.23e-581.23e-591.23e-601.23e-611.23e-621.23e-631.23e-641.23e-651.23e-661.23e-671.23e-681.23e-691.23e-701.23e-711.23e-721.23e-731.23e-741.23e-751.23e-761.23e-771.23e-781.23e-791.23e-801.23e-811.23e-821.23e-831.23e-841.23e-851.23e-861.23e-871.23e-881.23e-891.23e-901.23e-911.23e-921.23e-931.23e-941.23e-951.23e-961.23e-971.23e-981.23e-991.23e-1001.23e-1011.23e-1021.23e-1031.23e-1041.23e-1051.23e-1061.23e-1071.23e-1081.23e-1091.23e-1101.23e-1111.23e-1121.23e-1131.23e-1141.23e-1151.23e-1161.23e-1171.23e-1181.23e-1191.23e-1201.23e-1211.23e-1221.23e-1231.23e-1241.23e-1251.23e-1261.23e-1271.23e-1281.23e-1291.23e-1301.23e-1311.23e-1321.23e-1331.23e-1341.23e-1351.23e-1361.23e-1371.23e-1381.23e-1391.23e-1401.23e-1411.23e-1421.23e-1431.23e-1441.23e-1451.23e-1461.23e-1471.23e-1481.23e-1491.23e-1501.23e-1511.23e-1521.23e-1531.23e-1541.23e-1551.23e-1561.23e-1571.23e-1581.23e-1591.23e-1601.23e-1611.23e-1621.23e-1631.23e-1641.23e-1651.23e-1661.23e-1671.23e-1681.23e-1691.23e-1701.23e-1711.23e-1721.23e-1731.23e-1741.23e-1751.23e-1761.23e-1771.23e-1781.23e-1791.23e-1801.23e-1811.23e-1821.23e-1831.23e-1841.23e-1851.23e-1861.23e-1871.23e-1881.23e-1891.23e-1901.23e-1911.23e-1921.23e-1931.23e-1941.23e-1951.23e-1961.23e-1971.23e-1981.23e-1991.23e-2001.23e-2011.23e-2021.23e-2031.23e-2041.23e-2051.23e-2061.23e-2071.23e-2081.23e-2091.23e-2101.23e-2111.23e-2121.23e-2131.23e-2141.23e-2151.23e-2161.23e-2171.23e-2181.23e-2191.23e-2201.23e-2211.23e-2221.23e-2231.23e-2241.23e-2251.23e-2261.23e-2271.23e-2281.23e-2291.23e-2301.23e-2311.23e-2321.23e-2331.23e-2341.23e-2351.23e-2361.23e-2371.23e-2381.23e-2391.23e-2401.23e-2411.23e-2421.23e-2431.23e-2441.23e-2451.23e-2461.23e-2471.23e-2481.23e-2491.23e-2501.23e-2511.23e-2521.23e-2531.23e-2541.23e-2551.23e-2561.23e-2571.23e-2581.23e-2591.23e-2601.23e-2611.23e-2621.23e-2631.23e-2641.23e-2651.23e-2661.23e-2671.23e-2681.23e-2691.23e-2701.23e-2711.23e-2721.23e-2731.23e-2741.23e-2751.23e-2761.23e-2771.23e-2781.23e-2791.23e-2801.23e-2811.23e-2821.23e-2831.23e-2841.23e-2851.23e-2861.23e-2871.23e-2881.23e-2891.23e-2901.23e-2911.23e-2921.23e-2931.23e-2941.23e-2951.23e-2961.23e-2971.23e-2981.23e-2991.23e-3001.23e-3011.23e-3021.23e-3031.23e-3041.23e-3051.23e-3061.23e-3071.23e-3081.23e-3091.22999999999999e-3101.23000000000004e-3111.23000000000004e-3121.23000000000992e-3131.23000000015814e-3141.2300000021344e-3151.22999999225309e-3161.23000014047278e-3171.23000112860408e-3181.23002583188637e-3191.2302234581447e-3201.2302234581447e-3211.23516411460312e-3229.88131291682493e-3240



FishMonger
Veteran / Moderator

Sep 5, 2013, 9:45 AM


Views: 3372
Re: [smidhunraj] A small doubt on while loop

Sorry, I should have been more explicit.

I wanted you to print out both numbers on a single line so that you could see the progression.

Here's an adjusted version which should make it clear.


Code
#!usr/bin/perl 

# ALWAYS start your scripts with these 2 pragmas
use strict;
use warnings;

print "Enter the number: ";

# Use var names that describe the data that is being held
# and don't forget to use chomp to remove the line terminator
chomp (my $num = <>);

# define the value for the modulus operation
my $mod = 10;

while ($num > 0)
{
# use printf to output a cleanly formatted string
printf("%g %% %d = %d\n", $num, $mod, $num % $mod);

$num /= $mod;
}



smidhunraj
New User

Sep 5, 2013, 12:05 PM


Views: 3366
Re: [FishMonger] A small doubt on while loop

thanks a lot man that explanation clarifies my doubtSmile


smidhunraj
New User

Sep 6, 2013, 5:51 AM


Views: 3355
Re: [FishMonger] A small doubt on while loop

output seems to be

Code
Enter the number: 123 
123 % 10 = 3
12.3 % 10 = 2
1.23 % 10 = 1
0.123 % 10 = 0
0.0123 % 10 = 0
0.00123 % 10 = 0
0.000123 % 10 = 0
1.23e-05 % 10 = 0
1.23e-06 % 10 = 0
1.23e-07 % 10 = 0
1.23e-08 % 10 = 0
1.23e-09 % 10 = 0
1.23e-10 % 10 = 0
1.23e-11 % 10 = 0
1.23e-12 % 10 = 0
1.23e-13 % 10 = 0
1.23e-14 % 10 = 0
1.23e-15 % 10 = 0
1.23e-16 % 10 = 0
1.23e-17 % 10 = 0
1.23e-18 % 10 = 0
1.23e-19 % 10 = 0
1.23e-20 % 10 = 0
1.23e-21 % 10 = 0
1.23e-22 % 10 = 0
1.23e-23 % 10 = 0
1.23e-24 % 10 = 0
1.23e-25 % 10 = 0
1.23e-26 % 10 = 0
1.23e-27 % 10 = 0
1.23e-28 % 10 = 0
1.23e-29 % 10 = 0
1.23e-30 % 10 = 0
1.23e-31 % 10 = 0
1.23e-32 % 10 = 0
1.23e-33 % 10 = 0
1.23e-34 % 10 = 0
1.23e-35 % 10 = 0
1.23e-36 % 10 = 0
1.23e-37 % 10 = 0
1.23e-38 % 10 = 0
1.23e-39 % 10 = 0
1.23e-40 % 10 = 0
1.23e-41 % 10 = 0
1.23e-42 % 10 = 0
1.23e-43 % 10 = 0
1.23e-44 % 10 = 0
1.23e-45 % 10 = 0
1.23e-46 % 10 = 0
1.23e-47 % 10 = 0
1.23e-48 % 10 = 0
1.23e-49 % 10 = 0
1.23e-50 % 10 = 0
1.23e-51 % 10 = 0
1.23e-52 % 10 = 0
1.23e-53 % 10 = 0
1.23e-54 % 10 = 0
1.23e-55 % 10 = 0
1.23e-56 % 10 = 0
1.23e-57 % 10 = 0
1.23e-58 % 10 = 0
1.23e-59 % 10 = 0
1.23e-60 % 10 = 0
1.23e-61 % 10 = 0
1.23e-62 % 10 = 0
1.23e-63 % 10 = 0
1.23e-64 % 10 = 0
1.23e-65 % 10 = 0
1.23e-66 % 10 = 0
1.23e-67 % 10 = 0
1.23e-68 % 10 = 0
1.23e-69 % 10 = 0
1.23e-70 % 10 = 0
1.23e-71 % 10 = 0
1.23e-72 % 10 = 0
1.23e-73 % 10 = 0
1.23e-74 % 10 = 0
1.23e-75 % 10 = 0
1.23e-76 % 10 = 0
1.23e-77 % 10 = 0
1.23e-78 % 10 = 0
1.23e-79 % 10 = 0
1.23e-80 % 10 = 0
1.23e-81 % 10 = 0
1.23e-82 % 10 = 0
1.23e-83 % 10 = 0
1.23e-84 % 10 = 0
1.23e-85 % 10 = 0
1.23e-86 % 10 = 0
1.23e-87 % 10 = 0
1.23e-88 % 10 = 0
1.23e-89 % 10 = 0
1.23e-90 % 10 = 0
1.23e-91 % 10 = 0
1.23e-92 % 10 = 0
1.23e-93 % 10 = 0
1.23e-94 % 10 = 0
1.23e-95 % 10 = 0
1.23e-96 % 10 = 0
1.23e-97 % 10 = 0
1.23e-98 % 10 = 0
1.23e-99 % 10 = 0
1.23e-100 % 10 = 0
1.23e-101 % 10 = 0
1.23e-102 % 10 = 0
1.23e-103 % 10 = 0
1.23e-104 % 10 = 0
1.23e-105 % 10 = 0
1.23e-106 % 10 = 0
1.23e-107 % 10 = 0
1.23e-108 % 10 = 0
1.23e-109 % 10 = 0
1.23e-110 % 10 = 0
1.23e-111 % 10 = 0
1.23e-112 % 10 = 0
1.23e-113 % 10 = 0
1.23e-114 % 10 = 0
1.23e-115 % 10 = 0
1.23e-116 % 10 = 0
1.23e-117 % 10 = 0
1.23e-118 % 10 = 0
1.23e-119 % 10 = 0
1.23e-120 % 10 = 0
1.23e-121 % 10 = 0
1.23e-122 % 10 = 0
1.23e-123 % 10 = 0
1.23e-124 % 10 = 0
1.23e-125 % 10 = 0
1.23e-126 % 10 = 0
1.23e-127 % 10 = 0
1.23e-128 % 10 = 0
1.23e-129 % 10 = 0
1.23e-130 % 10 = 0
1.23e-131 % 10 = 0
1.23e-132 % 10 = 0
1.23e-133 % 10 = 0
1.23e-134 % 10 = 0
1.23e-135 % 10 = 0
1.23e-136 % 10 = 0
1.23e-137 % 10 = 0
1.23e-138 % 10 = 0
1.23e-139 % 10 = 0
1.23e-140 % 10 = 0
1.23e-141 % 10 = 0
1.23e-142 % 10 = 0
1.23e-143 % 10 = 0
1.23e-144 % 10 = 0
1.23e-145 % 10 = 0
1.23e-146 % 10 = 0
1.23e-147 % 10 = 0
1.23e-148 % 10 = 0
1.23e-149 % 10 = 0
1.23e-150 % 10 = 0
1.23e-151 % 10 = 0
1.23e-152 % 10 = 0
1.23e-153 % 10 = 0
1.23e-154 % 10 = 0
1.23e-155 % 10 = 0
1.23e-156 % 10 = 0
1.23e-157 % 10 = 0
1.23e-158 % 10 = 0
1.23e-159 % 10 = 0
1.23e-160 % 10 = 0
1.23e-161 % 10 = 0
1.23e-162 % 10 = 0
1.23e-163 % 10 = 0
1.23e-164 % 10 = 0
1.23e-165 % 10 = 0
1.23e-166 % 10 = 0
1.23e-167 % 10 = 0
1.23e-168 % 10 = 0
1.23e-169 % 10 = 0
1.23e-170 % 10 = 0
1.23e-171 % 10 = 0
1.23e-172 % 10 = 0
1.23e-173 % 10 = 0
1.23e-174 % 10 = 0
1.23e-175 % 10 = 0
1.23e-176 % 10 = 0
1.23e-177 % 10 = 0
1.23e-178 % 10 = 0
1.23e-179 % 10 = 0
1.23e-180 % 10 = 0
1.23e-181 % 10 = 0
1.23e-182 % 10 = 0
1.23e-183 % 10 = 0
1.23e-184 % 10 = 0
1.23e-185 % 10 = 0
1.23e-186 % 10 = 0
1.23e-187 % 10 = 0
1.23e-188 % 10 = 0
1.23e-189 % 10 = 0
1.23e-190 % 10 = 0
1.23e-191 % 10 = 0
1.23e-192 % 10 = 0
1.23e-193 % 10 = 0
1.23e-194 % 10 = 0
1.23e-195 % 10 = 0
1.23e-196 % 10 = 0
1.23e-197 % 10 = 0
1.23e-198 % 10 = 0
1.23e-199 % 10 = 0
1.23e-200 % 10 = 0
1.23e-201 % 10 = 0
1.23e-202 % 10 = 0
1.23e-203 % 10 = 0
1.23e-204 % 10 = 0
1.23e-205 % 10 = 0
1.23e-206 % 10 = 0
1.23e-207 % 10 = 0
1.23e-208 % 10 = 0
1.23e-209 % 10 = 0
1.23e-210 % 10 = 0
1.23e-211 % 10 = 0
1.23e-212 % 10 = 0
1.23e-213 % 10 = 0
1.23e-214 % 10 = 0
1.23e-215 % 10 = 0
1.23e-216 % 10 = 0
1.23e-217 % 10 = 0
1.23e-218 % 10 = 0
1.23e-219 % 10 = 0
1.23e-220 % 10 = 0
1.23e-221 % 10 = 0
1.23e-222 % 10 = 0
1.23e-223 % 10 = 0
1.23e-224 % 10 = 0
1.23e-225 % 10 = 0
1.23e-226 % 10 = 0
1.23e-227 % 10 = 0
1.23e-228 % 10 = 0
1.23e-229 % 10 = 0
1.23e-230 % 10 = 0
1.23e-231 % 10 = 0
1.23e-232 % 10 = 0
1.23e-233 % 10 = 0
1.23e-234 % 10 = 0
1.23e-235 % 10 = 0
1.23e-236 % 10 = 0
1.23e-237 % 10 = 0
1.23e-238 % 10 = 0
1.23e-239 % 10 = 0
1.23e-240 % 10 = 0
1.23e-241 % 10 = 0
1.23e-242 % 10 = 0
1.23e-243 % 10 = 0
1.23e-244 % 10 = 0
1.23e-245 % 10 = 0
1.23e-246 % 10 = 0
1.23e-247 % 10 = 0
1.23e-248 % 10 = 0
1.23e-249 % 10 = 0
1.23e-250 % 10 = 0
1.23e-251 % 10 = 0
1.23e-252 % 10 = 0
1.23e-253 % 10 = 0
1.23e-254 % 10 = 0
1.23e-255 % 10 = 0
1.23e-256 % 10 = 0
1.23e-257 % 10 = 0
1.23e-258 % 10 = 0
1.23e-259 % 10 = 0
1.23e-260 % 10 = 0
1.23e-261 % 10 = 0
1.23e-262 % 10 = 0
1.23e-263 % 10 = 0
1.23e-264 % 10 = 0
1.23e-265 % 10 = 0
1.23e-266 % 10 = 0
1.23e-267 % 10 = 0
1.23e-268 % 10 = 0
1.23e-269 % 10 = 0
1.23e-270 % 10 = 0
1.23e-271 % 10 = 0
1.23e-272 % 10 = 0
1.23e-273 % 10 = 0
1.23e-274 % 10 = 0
1.23e-275 % 10 = 0
1.23e-276 % 10 = 0
1.23e-277 % 10 = 0
1.23e-278 % 10 = 0
1.23e-279 % 10 = 0
1.23e-280 % 10 = 0
1.23e-281 % 10 = 0
1.23e-282 % 10 = 0
1.23e-283 % 10 = 0
1.23e-284 % 10 = 0
1.23e-285 % 10 = 0
1.23e-286 % 10 = 0
1.23e-287 % 10 = 0
1.23e-288 % 10 = 0
1.23e-289 % 10 = 0
1.23e-290 % 10 = 0
1.23e-291 % 10 = 0
1.23e-292 % 10 = 0
1.23e-293 % 10 = 0
1.23e-294 % 10 = 0
1.23e-295 % 10 = 0
1.23e-296 % 10 = 0
1.23e-297 % 10 = 0
1.23e-298 % 10 = 0
1.23e-299 % 10 = 0
1.23e-300 % 10 = 0
1.23e-301 % 10 = 0
1.23e-302 % 10 = 0
1.23e-303 % 10 = 0
1.23e-304 % 10 = 0
1.23e-305 % 10 = 0
1.23e-306 % 10 = 0
1.23e-307 % 10 = 0
1.23e-308 % 10 = 0
1.23e-309 % 10 = 0
1.23e-310 % 10 = 0
1.23e-311 % 10 = 0
1.23e-312 % 10 = 0
1.23e-313 % 10 = 0
1.23e-314 % 10 = 0
1.23e-315 % 10 = 0
1.23e-316 % 10 = 0
1.23e-317 % 10 = 0
1.23e-318 % 10 = 0
1.23003e-319 % 10 = 0
1.23022e-320 % 10 = 0
1.23022e-321 % 10 = 0
1.23516e-322 % 10 = 0
9.88131e-324 % 10 = 0

why is that loop running these many times........


FishMonger
Veteran / Moderator

Sep 6, 2013, 7:05 AM


Views: 3350
Re: [smidhunraj] A small doubt on while loop


Quote
why is that loop running these many times........


Because it took that many iterations for $num to become small enough (but still greater than 0) for perl (due to rounding) to evaluate it as 0.

This is an infinite loop because $num will never be less than 0.

Code
# $num started out as a positive int 
while ($num > 0)
{
$num /= $mod;
}


However, due to the default level of precision and rounding that occurs behind the scenes, it took (on my system) 326 iterations for the number to be small enough (but still greater than 0) for perl to evaluate it as 0.

To bypass the default rounding and see that you have an infinate loop, you can add the bignum pragma module.


Code
#!usr/bin/perl 

# ALWAYS start your scripts with these 2 pragmas
use strict;
use warnings;
#use bignum; # uncomment this line to see the infinite loop

print "Enter the number: ";
chomp (my $num = <>);

my $mod = 10;
my $i;
while ($num > 0)
{
$i++;
printf("Iteration %d: %g %% %d = %d\n", $i, $num, $mod, $num % $mod);

$num /= $mod;
}


There are several ways to fix this so that you don't have an infinite loop, and the chosen solution will depend on the details of the specification for this homework assignment.