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

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
• 6
• 3
1 Solution

Author Commented:
Adjusted points from 25 to 125
0

Commented:
#!/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+\$/){
exit;
}

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

Author Commented:
This question has a deletion request Pending
0

Commented:
This question no longer is pending deletion
0

Commented:
why have you requested a delete for this Q??

0

Commented:
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+\$/){
exit;
}

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

Commented:
did you get a chance to try out the solution??

did it work??

let me know.

0

Commented:
Hmm....

This really looks like someones homework to me.

0

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

michelle
0

Commented:
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.