PERL   36

gcd perl

Guest on 20th June 2022 04:37:23 PM

  1. #!/usr/bin/perl -wT
  2. use CGI qw(:standard);
  3. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  4. use strict;
  5.  
  6. sub gcd {
  7.     my ($x, $y) = ($_[0], $_[1]);
  8.     while ($x > 0 && $y > 0) {
  9.         if ($x > $y) {
  10.             $x = $x % $y;
  11.         }
  12.         else {
  13.             $y = $y % $x;
  14.         }
  15.     }
  16.     return $x + $y;
  17. }
  18.  
  19. print header;
  20. print start_html("CGI script for the GCD");
  21. print h1("Result");
  22.  
  23. my $x = param("x");
  24. my $y = param("y");
  25. my $result = gcd($x, $y);
  26.  
  27. print p("The GCD of $x and $y is $result.</p>\n");
  28. print end_html;

Raw Paste


Login or Register to edit or fork this paste. It's free.