Solved

perl prob 1

Posted on 2000-04-17
4
157 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 25 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

773 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