1 ffd9a51f 2023-08-04 jrmu #!/usr/bin/perl
3 ffd9a51f 2023-08-04 jrmu # Write a program that computes the circumference of a circle with a radius of
4 ffd9a51f 2023-08-04 jrmu # 12.5. Circumference is 2π times the radius (approximately 2 times
5 ffd9a51f 2023-08-04 jrmu # 3.141592654). The answer you get should be about 78.5.
7 ffd9a51f 2023-08-04 jrmu # Modify the program from the previous exercise to prompt for and accept a
8 ffd9a51f 2023-08-04 jrmu # radius from the person running the program. So, if the user enters 12.5 for
9 ffd9a51f 2023-08-04 jrmu # the radius, she should get the same number as in the previous exercise.
11 ffd9a51f 2023-08-04 jrmu # Modify the program from the previous exercise so that, if the user enters a
12 ffd9a51f 2023-08-04 jrmu # number less than zero, the reported circumference will be zero, rather than
15 ffd9a51f 2023-08-04 jrmu use warnings;
19 ffd9a51f 2023-08-04 jrmu sub circum {
20 ffd9a51f 2023-08-04 jrmu my $π = 3.141592654;
21 ffd9a51f 2023-08-04 jrmu my ($radius) = @_;
22 ffd9a51f 2023-08-04 jrmu if ($radius < 0) {
25 ffd9a51f 2023-08-04 jrmu return 2*$π*$radius;
28 ffd9a51f 2023-08-04 jrmu print "Radius: ";
30 ffd9a51f 2023-08-04 jrmu chomp(my $radius = <STDIN>);
32 ffd9a51f 2023-08-04 jrmu print "circumference = " . circum($radius) . "\n";