Last updated on February 14th, 2022 at 08:14 am

This script will show how to read numbers from keyboard and find the sum. To exit the program all you have to do is add 999. Here is the script. For your convenience I have added a sample output as well.

#!/usr/bin/perl
print "Enter the number \n";
$userinput = <STDIN>;
chomp ($userinput);
print "User typed $userinput\n";
while ($userinput != 999) {
$sum=$sum+$userinput;
print "The SUM is $sum\n";
print "Enter the number again \n";
$userinput = <STDIN>;
chomp ($userinput);
}
print "Typed 999, So exiting\n";

Sample Output

:~# ./test.pl
Enter the number
10
User typed 10
The SUM is 10
Enter the number again
20
The SUM is 30
Enter the number again
10
The SUM is 40
Enter the number again
4
The SUM is 44
Enter the number again
45
The SUM is 89
Enter the number again
100
The SUM is 189
Enter the number again
999
Typed 999, So exiting
:~# 

Leave a Reply

Your email address will not be published. Required fields are marked *