You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

17 lines
482 B
Perl

#!/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");