programming-examples/perl/Statement/Sums the numbers from 1 to a specified number and also sums the even numbers.pl

17 lines
482 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
#!/usr/local/bin/perl
print ("Enter the last number in the sum:\n");
$limit = <STDIN>;
chop ($limit);
$count = 1;
$total = $eventotal = 0;
for ($count = 1; $count <= $limit; $count++) {
$total += $count;
if ($count % 2 == 1) {
# start the next iteration if the number is odd
next;
}
$eventotal += $count;
}
print("The sum of the numbers 1 to $limit is $total\n");
print("The sum of the even numbers is $eventotal\n");