Perl Programming/Exercise 1

From Wikibooks, open books for an open world
< Perl Programming
Jump to: navigation, search

Getting started, displaying text[edit]

Task: Create a Perl program which displays "Hello world" and then exits.

#!/usr/bin/perl

print "Hello world";

Displaying numbers[edit]

Try 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[edit]

Write 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[edit]

Put 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.


Answers

Next exercise