Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

Perl programming problem 2

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
myoreorex
Asked:
myoreorex
  • 6
  • 3
1 Solution
 
myoreorexAuthor Commented:
Adjusted points from 25 to 125
0
 
maneshrCommented:
#!/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
 
myoreorexAuthor Commented:
This question has a deletion request Pending
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
maneshrCommented:
This question no longer is pending deletion
0
 
maneshrCommented:
why have you requested a delete for this Q??

0
 
maneshrCommented:
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
 
maneshrCommented:
did you get a chance to try out the solution??

did it work??

let me know.

0
 
jkstillCommented:
Hmm....

This really looks like someones homework to me.

0
 
myoreorexAuthor Commented:
Thank you for being the nicest most professional one.

michelle
0
 
maneshrCommented:
most welcome to be of help to you. :-)

Rgds
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now