In: Computer Science
Using Perl;
Part 1:
Allow the user to enter a full name in the “first last”
format
Print just the first name
Print just the last name
Print the name in “last, first” format
Print the entire name out in all capital letters
Use a single print statement to print out the first name on one
line and the last name on
the next line.
There should be 5 print statements generating 6 lines of
output.
Part 2:
Enter a three digit number (such as 316)
Print out the number from the hundreds place (for example, 3 in
316)
Print out the number from the ten’s place (for example, 1 in
316)
Print out the number from the one’s place (for example, 6 in
316)
Print out the original number
All output will be appropriately labeled and on a separate line,
such as “hundreds
place”, “tens place”, and so on.
ALL the numeric values MUST be right justified in the output using
formatted output
All work done for this part MUST treat the information as numeric
values – do not treat
the input as a string or as an array.
There should be 4 print statements generating 4 lines of
output.
Solution for Part1:
Code:
# allow the user to enter full name
print "Enter full name in format. First Last: ";
chomp($full_name = <>);
#Print just the first name
$index = index($full_name, ' ');
$first_name = substr($full_name, 0, $index);
print "$first_name\n";
#Print just the last name
$last_name = substr($full_name, $index + 1);
print "$last_name\n";
#Print the name in “last, first” format
print "$last_name, $first_name\n";
#Print the entire name out in all capital letters
$full_name_in_caps = "\U$full_name";
print "$full_name_in_caps\n";
#Use a single print statement to print out the first name on one
line and the
#last name on the next line.
print "$first_name\n$last_name";
Output:
Solution for Part 2:
First get the user input. Use modulus operator to get the one's place digit. divide the number with 10 to get quotient. Now again perform modulus operation to get tens' place. divide the number with 10 to gt quotient. The quotient is the hundreds place digit. Use sprintf function to format the output. 3 places ("%3d") are used to right justify the output.
Code:
#Enter a three digit number (such as 316)
print "Enter a 3 digit number: ";
chomp($num = <>);
# make an copy of original number
$num2 = $num;
#evaluate the ones place using modulus % operator
#spritf used to format the output to right justify 3 places
$ones_place = sprintf("%3d",$num2 % 10);
#divide the num2 with 10 so that we can get quotent
$num2 = $num2 / 10;
$tens_place = sprintf("%3d",$num2 % 10);
#divide the num2 with 10 so that we can get quotent
$num2 = $num2 / 10;
# no need to use modulus operator here as we are left with
single digit only in num2
$hundreds_place = sprintf("%3d",$num2);
#Print out the number from the hundreds place (for example, 3 in
316)
print "Hundreds place:\t\t$hundreds_place\n";
#Print out the number from the ten’s place (for example, 1 in
316)
print "Tens place:\t\t$tens_place\n";
#Print out the number from the one’s place (for example, 6 in
316)
print "Ones place:\t\t$ones_place\n";
#Print out the original number
print "Original number:\t$num";
Output: