Saturday, May 31, 2014

Compare two numbers

Script

#!/usr/bin/perl

print ("Enter a number: ");
$first_number = <STDIN>;
chop ($first_number);
print ("Enter another number: ");
$second_number = <STDIN>;
chop ($second_number);

if ($first_number == $second_number)
{
        print ("The two numbers are equal. \n");
}
        elsif ($first_number == $second_number + 1)
        {
                print ("The first number is greater than the second number by one. \n");
        }
        elsif ($first_number == $second_number - 1)
        {
                print ("The second number is greater than the first number by one. \n");
        }
else
{
        print ("The two numbers are not equal. \n");
}

print ("This is the last line of the program. \n");


Execution

Enter a number: 75
Enter another number: 75
The two numbers are equal.
This is the last line of the program.

Enter a number: 54
Enter another number: 55
The second number is greater than the first number by one.
This is the last line of the program.

Enter a number: 33
Enter another number: 37
The two numbers are not equal.
This is the last line of the program.

Enter a number: 47
Enter another number: 42
The two numbers are not equal.
This is the last line of the program.

Enter a number: 63
Enter another number: 62
The first number is greater than the second number by one.
This is the last line of the program.



No comments:

Post a Comment