# Sums the numbers from 1 to a specified number and also sums the even numbers. : For « Statement « Perl

Home
Perl
 1 Array 2 CGI 3 Class 4 Data Type 5 Database 6 File 7 GUI 8 Hash 9 Language Basics 10 Network 11 Regular Expression 12 Report 13 Statement 14 String 15 Subroutine 16 System Functions 17 Win32 18 XML
 Perl » Statement » For
Sums the numbers from 1 to a specified number and also sums the even numbers.
 ```     #!/usr/local/bin/perl  print ("Enter the last number in the sum:\n");  \$limit = ;  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");                       ```
Related examples in the same category
 1 A program that prints the numbers from 1 to 5 using the for statement. 2 Create an infinite loop using a for loop: 3 Here is an example of the for loop 4 Initialization, test, and increment, decrement of counters is done in one step 5 Loop Control Statements 6 Looping through an array with the for repetition structure 7 Nested for loop 8 Put more than one statement first and third part of for statement 9 Reference array length in for loop 10 Reference for loop control variable 11 Standard for structure: Loops 5 times, printing the multiples of 5 from 0-20 12 The for Loop 13 The or part allows for a statement to be executed if the main part fails. 14 Use for loop to output all elements in an array 15 Use for loop to output the element one by one in an array 16 Uses the for statement to read four input lines and write three of them. 17 Using block labels with next. 18 Using for loop to sum the total 19 Using my inside for statement 20 Using nested for loop to assign value to two dimensional array 21 Using nested for loop to output the elements in a two-dimensional array 22 You can use a for loop to loop for a specific number of times. 23 Mixed diamond operator with for loop condition 24 Mixed diamond operator with for loop condition 2