# 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.

def to_celsius()
    puts "Enter Fahrenheit temperature:"
    f = gets.chomp.to_f
    c = (f - 32) * 5 / 9
    puts (f).to_s + "° Fahrenheit is " + (c).to_s + "° Celsius"
end

def to_fahrenheit()
    puts "Enter Celsius temperature:"
    c = gets.chomp.to_f
    f = c * 9 / 5 + 32
    puts (c).to_s + "° Celsius is " + (f).to_s + "° Fahrenheit"
end

def if_else()
    puts "Enter F to convert to Fahrenheit or C to convert to Celsius:"
    choice = gets.chomp
    if choice == "C" || choice == "c"
        to_celsius()
    elsif choice == "F" || choice == "f"
        to_fahrenheit()
    else
        puts "You must enter C to convert to Celsius or F to convert to Fahrenheit!"
    end
end

def case_when()
    puts "Enter F to convert to Fahrenheit or C to convert to Celsius:"
    choice = gets.chomp
    case choice
        when "C"
            to_celsius()
        when "c"
            to_celsius()
        when "F"
            to_fahrenheit()
        when "f"
            to_fahrenheit()
        else
            puts "You must enter C to convert to Celsius or F to convert to Fahrenheit!"
    end
end

def main()
    if_else()
    case_when()
end

main()

कोशिश करो

सम्पादन

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

यह भी देखें

सम्पादन