Here's a Perl script that will answer the problem for any number of heads and feet. sub how_many { print "Number of heads: "; chomp($h = ); print "Number of feet: ";chomp($f = ); $p = $h; $c = 0; while (($p*4 + $c*2) != $f) { $p--; $c++; } } &how_many; print "There are $p pigs and $c chickens.\n"; print "They have $h heads and $f feet.\n"; Number of heads: 27 Number of feet: 78 There are 12 pigs and 15 chickens. They have 27 heads and 78 feet.
Chat with our AI personalities