Blame


1 ffd9a51f 2023-08-04 jrmu #!/usr/bin/perl
2 ffd9a51f 2023-08-04 jrmu
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.
6 ffd9a51f 2023-08-04 jrmu #
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.
10 ffd9a51f 2023-08-04 jrmu #
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
13 ffd9a51f 2023-08-04 jrmu # negative.
14 ffd9a51f 2023-08-04 jrmu
15 ffd9a51f 2023-08-04 jrmu use warnings;
16 ffd9a51f 2023-08-04 jrmu use strict;
17 ffd9a51f 2023-08-04 jrmu use utf8;
18 ffd9a51f 2023-08-04 jrmu
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) {
23 ffd9a51f 2023-08-04 jrmu return 0;
24 ffd9a51f 2023-08-04 jrmu }
25 ffd9a51f 2023-08-04 jrmu return 2*$π*$radius;
26 ffd9a51f 2023-08-04 jrmu }
27 ffd9a51f 2023-08-04 jrmu
28 ffd9a51f 2023-08-04 jrmu print "Radius: ";
29 ffd9a51f 2023-08-04 jrmu
30 ffd9a51f 2023-08-04 jrmu chomp(my $radius = <STDIN>);
31 ffd9a51f 2023-08-04 jrmu
32 ffd9a51f 2023-08-04 jrmu print "circumference = " . circum($radius) . "\n";