Solved

perl prob 1

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now