Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

perl prob 1

Posted on 2000-04-17
4
Medium Priority
?
167 Views
Last Modified: 2010-03-05



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.
0
Comment
Question by:myoreorex
  • 2
4 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2724684
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";
0
 
LVL 3

Expert Comment

by:jyu_88
ID: 2724686
#! /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??!!!
0
 
LVL 5

Accepted Solution

by:
thoellri earned 100 total points
ID: 2725406
print 1+2+3+4+5,"\n";

That's much quicker :-)
  Tobias

PS: And you even optimize it:
 
 print 15,"\n";

0
 
LVL 16

Expert Comment

by:maneshr
ID: 2728136
did you get a chance to try out the solution??

did it work??

let me know.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question