Solved

perl prob 1

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

789 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