본문 바로가기

Programming language/C++

[C++] 온도 변환기 구현

안녕하세요 오늘은 온도 변환기 입니다. (화씨 <-> 섭씨)

#include <iostream>
using namespace std;

int main() {
    int choice;
    double temperature;

    cout << "Temperature Converter" << endl;
    cout << "1. Celsius to Fahrenheit" << endl;
    cout << "2. Fahrenheit to Celsius" << endl;
    cout << "Enter your choice (1 or 2): ";
    cin >> choice;

    if (choice != 1 && choice != 2) {
        cout << "Invalid choice. Please choose 1 or 2." << endl;
        return 1; // Exit with an error code
    }

    if (choice == 1) {
        cout << "Enter temperature in Celsius: ";
        cin >> temperature;
        double fahrenheit = (temperature * 9.0 / 5.0) + 32.0;
        cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
    } else {
        cout << "Enter temperature in Fahrenheit: ";
        cin >> temperature;
        double celsius = (temperature - 32.0) * 5.0 / 9.0;
        cout << "Temperature in Celsius: " << celsius << endl;
    }

    return 0; // Exit successfully
}

결과 값

Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice (1 or 2): 1
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77