PERL   34

gcd

Guest on 20th June 2022 04:38:05 PM

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. unsigned long gcd (unsigned long x, unsigned long y)
  5. {
  6.   while (x > 0 && y > 0)
  7.     if (x > y) x %= y; else y %= x;
  8.   return x + y;
  9. }
  10.  
  11. int main ()
  12. {
  13.   printf("Content-type: text/html\n\n");
  14.   printf("<html>\n"
  15.          "<head>\n"
  16.          "<title>CGI script for the GCD</title>\n"
  17.          "</head>\n"
  18.          "<body>\n"
  19.          "<h1>Result</h1>\n");
  20.  
  21.   char *data = getenv("QUERY_STRING");
  22.   unsigned long x, y;
  23.  
  24.   if (data == NULL)
  25.     printf("<p>Error! Error in passing data from form to script.</p>");
  26.   else if (sscanf(data, "x=%lu&y=%lu", &x, &y) != 2)
  27.     printf("<p>Error! Invalid parameters. Data must be numeric.</p>");
  28.   else
  29.     printf("<p>The GCD of %lu and %lu is %lu.</p>\n", x, y, gcd(x, y));
  30.  
  31.   printf("</body>\n"
  32.          "</html>\n");
  33.   return 0;
  34. }

Raw Paste


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