Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C#을 사용해 섭씨를 화씨로 변환하는 프로그램 만들기

온도 단위 변환은 프로그래밍 학습 과정에서 자주 접하게 되는 기본 예제 중 하나입니다. 이 글에서는 C#을 활용해 섭씨(Celsius) 온도를 화씨(Fahrenheit)로 변환하는 방법을 소개합니다.

1. 섭씨 온도 설정하기

먼저 변환할 섭씨 온도 값을 변수에 저장합니다.

double celsius = 36;
Console.WriteLine("Celsius: " + celsius);

2. 섭씨를 화씨로 변환하기

섭씨를 화씨로 변환하는 공식은 다음과 같습니다.

fahrenheit = (celsius * 9) / 5 + 32;

즉, 섭씨 값에 9를 곱한 뒤 5로 나누고, 여기에 32를 더하면 화씨 온도가 계산됩니다.

3. 전체 예제 코드

아래의 완성된 코드를 실행하여 직접 변환 결과를 확인해 보세요.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo {

    class MyApplication {

        static void Main(string[] args) {

            double fahrenheit;

            double celsius = 36;
            Console.WriteLine("Celsius: " + celsius);

            fahrenheit = (celsius * 9) / 5 + 32;
            Console.WriteLine("Fahrenheit: " + fahrenheit);

            Console.ReadLine();
        }
    }
}

실행 결과

Celsius: 36
Fahrenheit: 96.8

코드를 실행하면 36도 섭씨가 96.8도 화씨로 변환되어 출력되는 것을 확인할 수 있습니다. 이처럼 간단한 산술 연산만으로도 C#에서 손쉽게 온도 단위를 변환할 수 있습니다.