Link to home
Start Free TrialLog in
Avatar of myoreorex
myoreorex

asked on

perl prob 1




Write a program in Perl that calculates the sum of the integers from 1 to 5 using a statement that simply adds the numbers together and prints the sum. This isn't too difficult for a small number of addends.
Avatar of maneshr
maneshr

here is your code..

you can change $end to any number.

#!/usr/local/bin/perl

$start=1;
$end=5;
$sum=0;

foreach($start .. $end){
  $sum+=$_;
}

print $sum,"\n";
#! /usr/bin/perl -w
my $sum = 0;
my $i;
foreach $i (1..5) {
      $sum += $i;
}
print "sum=$sum\n";
exit 0

This is not your homework, right??!!!
ASKER CERTIFIED SOLUTION
Avatar of thoellri
thoellri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
did you get a chance to try out the solution??

did it work??

let me know.