#!/usr/bin/perl

# This program asks the user to select Fahrenheit or Celsius conversion
# and input a given temperature. Then the program converts the given
# temperature and displays the result.

sub to_celsius
{
    my $f;
    my $c;
    
    print "Enter Fahrenheit temperature:", "\n";
    $f = <>;
    chomp($f);
    $c = ($f - 32) * 5 / 9;
    print $f . "° Fahrenheit is " . $c . "° Celsius", "\n";
}

sub to_fahrenheit
{
    my $c;
    my $f;
    
    print "Enter Celsius temperature:", "\n";
    $c = <>;
    chomp($c);
    $f = $c * 9 / 5 + 32;
    print $c . "° Celsius is " . $f . "° Fahrenheit", "\n";
}

sub if_else
{
    my $choice;
    
    print "Enter F to convert to Fahrenheit or C to convert to Celsius:", "\n";
    $choice = <>;
    chomp($choice);
    if ($choice eq "C" || $choice eq "c") 
    {
        to_celsius();
    }
    elsif ($choice eq "F" || $choice eq "f") 
    {
        to_fahrenheit();
    }
    else 
    {
        print "You must enter C to convert to Celsius or F to convert to Fahrenheit!", "\n";
    }
}

sub main
{
    if_else()
}

main()

कोशिश करो

सम्पादन

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें

सम्पादन