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.
#! /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??!!!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
did you get a chance to try out the solution??
did it work??
let me know.
did it work??
let me know.
you can change $end to any number.
#!/usr/local/bin/perl
$start=1;
$end=5;
$sum=0;
foreach($start .. $end){
$sum+=$_;
}
print $sum,"\n";