In this post, you are going to see how to write a Perl CGI script to find the Entered number is Armstrong or not.
The same program is written in other languages such as Java, Python, C, C#, and PHP, just give a search on this site, use “Armstrong” as the keyword.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#!/usr/bin/perl -w ##these two lines are used to display errors on browser. If you don't want, you can remove these use CGI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); ##error display ends ##below line is important if you want to display the result in browser print "Content-type: text/html\n\n"; ##HTML Part print "<html> <head><title>Perl CGI Script to find Armstrong Number</title></head> <body> <h3>Web Based Perl script to Find Armstrong Number - <a href='http://www.tutorialsmade.com/'>Tutorialsmade.com</a></h3> <form action='' method='post'> <label>Enter a number to check <label> <input type='text' name='number' /> <input type='submit' value='Submit' /> </form> </body> </html>"; ## here is the perl logic starts my $q = new CGI; ## check if a form is submitted with $q->param() if($q->param()) { $number = $q->param('number'); $temp = $number; $sum = 0; ##loop till $temp becomes 0 while($temp != 0) { $rem = $temp % 10; ##find the reminder $sum = $sum + ($rem * $rem * $rem); ##add $sum and $rem x 3 $temp = $temp / 10; ##find the quotient for the next loop } ##if entered number and $sum is same then it is armstrong number if($number == $sum) { print "<p>Armstrong Number</p>"; } else { print "<p>Not an Armstrong Number</p>"; } } |
Read the comments in the script to understand.
Enjoy the day!