Solved

Perl programming problem 2

Posted on 2000-04-17
10
154 Views
Last Modified: 2010-03-05
Use the fallowing algebraic formula to write a program in perl that calculates and prints the sum of the integers from 1 to n where n is obtained as user input from the keyboard.

The formula is:  sum of 1 to n:  n x (n+1)
0
Comment
Question by:myoreorex
  • 6
  • 3
10 Comments
 

Author Comment

by:myoreorex
ID: 2724419
Adjusted points from 25 to 125
0
 
LVL 16

Accepted Solution

by:
maneshr earned 125 total points
ID: 2724420
#!/usr/local/bin/perl

print "Enter a number ";
$user_input=<STDIN>;
chomp($user_input); ## Remove the \n from the users input

$user_input=~ s/\s+//g; ## Remove any white spaces from the user's input.

if ($user_input!~ /^[+-]?\d+$/){
  print "Please enter ONLY integers\n";
  exit;
}

$summ=$user_input * ($user_input+1);
print $summ,"\n";
0
 

Author Comment

by:myoreorex
ID: 2724522
This question has a deletion request Pending
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:maneshr
ID: 2724709
This question no longer is pending deletion
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2724710
why have you requested a delete for this Q??

0
 
LVL 16

Expert Comment

by:maneshr
ID: 2724719
i think this is what you might want...

#!/usr/local/bin/perl

print "Enter a number ";
$user_input=<STDIN>;
chomp($user_input); ## Remove the \n from the users input
$user_input=~ s/\s+//g; ## Remove any white spaces from the user's input.
if ($user_input!~ /^[+-]?\d+$/){
  print "Please enter ONLY integers\n";
  exit;
}

foreach (1..$user_input){
  $ans+=$_;
}
print $ans,"\n";
0
 
LVL 16

Expert Comment

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

did it work??

let me know.

0
 
LVL 3

Expert Comment

by:jkstill
ID: 2735498
Hmm....

This really looks like someones homework to me.

0
 

Author Comment

by:myoreorex
ID: 2735613
Thank you for being the nicest most professional one.

michelle
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2740441
most welcome to be of help to you. :-)

Rgds
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PERL get the value for query 4 151
Perl Sort Question 4 135
Perl script to parse xml file 2 113
iSeries PERL Scripts 7 158
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 will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

777 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