Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Perl Programming/Exercise 1

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Getting started, displaying text Task: Create a Perl program which displays "Hello world" and then exits. #!/usr/bin/perl print "Hello world"; exit(); Displaying numbers 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 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 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