Perl Exercises
1. Create a script that asks the user for a number and shows the double.2. Create a script that prompts the user for a number and displays its square.
3. Create a script that asks for the radius of a circle and displays the diameter, length of the circle, and the area of the circle.
4. Create a Perl script that prompts the user for two grades and displays the average.
5. Do the same as the previous exercise, but with 3 notes.
6. Make a script that simulates a calculator in Perl. It should prompt the user for two numbers and then display the result of the sum, subtraction, multiplication, division, average and the remainder of the division of the first by the second.
7. Create two scripts, one that takes the temperature in Celsius and converts it to Fahrenheit, and one that does the opposite. The mathematical formulas are:
- !!! ATTENTION !!!
But I strongly suggest that you try to solve the above questions, try hard, think, research, try again ... if you make a mistake or the code comes out ugly and big, no problem, IT'S SAME at the beginning!
It's part of the journey!
Insist, break your head, this is how you become a good programmer and not watching Youtube videos of people programming, learn is really trying, the good ones are forged like this, in the race, in the attempt, ok?
Below, the solutions.
Commented Solutions in Perl
1.#!/usr/bin/perl print "Number: "; $num = <STDIN>; chomp($num); print "Double: ",2*$num, "\n";2.
#!/usr/bin/perl print "Number: "; $num = <STDIN>; chomp($num); print "Square: ",$num*$num, "\n";3. The diameter formula is twice the radius, let's store the radius in the $rad variable.
In length is: 2 x pi x radius, which in Perl is: 2 * 3.14 * $rad
Area formula: pi x radius² = 3.14 * $ rad * $ rad
#!/usr/bin/perl print "Radius: "; $rad = <STDIN>; chomp($rad); print "Diameter: ", 2*$rad, "\n"; print "Length : ", 2*3.14*$rad, "\n"; print "Area : ", 3.14*$rad*$rad, "\n";
4. Let's store the grades in the $num1 and $num2 variables.
The average formula is: ($num1 + $num2)/2
Be careful to not to do: $num1 + $ num2 / 2, if you do, by the operator precedence rule, the division will be done first, in the $num2 variable, and then the sum, doing the wrong calculation.
To make no mistake and leave the code well organized, use parentheses, ok?
#!/usr/bin/perl print "Grade 1: "; $num1 = <STDIN>; print "Grade 2: "; $num2 = <STDIN>; chomp($num1); chomp($num2); $media = ($num1 + $num2)/2; print "Average: ",$media, "\n";Can you do it using fewer variables?
5.
#!/usr/bin/perl print "Grade 1: "; $num1 = <STDIN>; print "Grade 2: ";$num2 = <STDIN>; print "Grade 3: ";$num3 = <STDIN>; chomp($num1); chomp($num2); chomp($num3); $media = ($num1 + $num2 + $num3)/3; print "Average: ",$media, "\n";6.
#!/usr/bin/perl print "First : "; $num1 = <STDIN>; print "Second : "; $num2 = <STDIN>; chomp($num1); chomp($num2); print "Sum : ",$num1+$num2, "\n"; print "Subtraction : ",$num1-$num2, "\n"; print "Multiplication: ",$num1*$num2, "\n"; print "Division : ",$num1/$num2, "\n"; print "Average : ",($num1+$num2)/2, "\n"; print "Remainde : ",$num1%$num2, "\n";7.
Celsius to Fahrenheit:
#!/usr/bin/perl print "Degrees in Celsius: "; $C = <STDIN>; chomp($C); $F = (9*$C/5) + 32; print "Degrees in Fahrenheit: ",$F, "\n";Fahrenheit to Celsius:
#!/usr/bin/perl print "Fahrenheit: "; $F = <STDIN>; chomp($F); $C = 5*($F-32)/9; print "Celsius: ",$C, "\n";
No comments:
Post a Comment