Perl Programming/Exercise 1
Getting started, displaying text
editTask: Create a Perl program which displays "Hello world" and then exits.
#!/usr/bin/perl
print "Hello world";
exit();
Displaying numbers
editTry doing these few excercises yourself
- Put the number 4000/7 into a variable, and display it on screen.
- Adapt the program to display the number to 3 decimal places. (Hint: The well-known printf works in Perl!)
- Adapt the program to round up if the last decimal place is greater than 5
- Adapt the program to display the number with leading zeros
- Adapt the program to show a + sign if the number is positive, and check that it correctly displays a - sign if you subtract 1000 from the number.
#!/usr/bin/perl
my $divisionResult = 4000/7;
print "Result of 4000 divided by 7 is $divisionResult";
Functions
editWrite a function to calculate the roots of a Quadratic equation, where you give the coefficients a,b,c to the function, and it returns both the values of x
Putting it all together
editPut together these programs into one which:
- Displays a title on screen
- Creates 3 random numbers (a,b,c)
- Displays those numbers neatly formatted
- Uses those numbers as the coefficients of a quadratic equation, calculates and displays both roots.