C 언어로 원기둥 표면적 계산하기

CBeginner
지금 연습하기

소개

이 실습에서는 C 프로그래밍을 사용하여 원기둥의 표면적을 계산하는 방법을 배웁니다. 이 실습에서는 사용자로부터 원기둥의 반지름과 높이를 입력받고, 공식 2PIr*(r+h) 를 사용하여 표면적을 계산하는 단계를 다룹니다. 그런 다음 프로그램은 계산된 표면적을 출력합니다.

이 실습은 원기둥의 표면적 계산 과정을 이해하는 데 필요한 C 코드를 포함하여 단계별 가이드를 제공합니다. 이 실습을 마치면 C 프로그래밍을 사용하여 기하학적 계산을 수행하는 방법에 대한 확실한 이해를 얻게 될 것입니다.

반지름과 높이 읽기

이 단계에서는 C 프로그래밍을 사용하여 원기둥의 반지름과 높이를 읽는 방법을 배웁니다. 사용자에게 값을 입력하도록 요청하는 간단한 프로그램을 만들 것입니다.

먼저 프로젝트 디렉토리에 새로운 C 파일을 생성합니다.

cd ~/project
nano cylinder_surface_area.c

이제 다음 코드를 파일에 추가합니다.

#include <stdio.h>

int main() {
    float radius, height;

    // 사용자에게 반지름 입력 요청
    printf("Enter the radius of the cylinder: ");
    scanf("%f", &radius);

    // 사용자에게 높이 입력 요청
    printf("Enter the height of the cylinder: ");
    scanf("%f", &height);

    // 입력된 값 출력
    printf("Radius: %.2f\n", radius);
    printf("Height: %.2f\n", height);

    return 0;
}

코드를 살펴보겠습니다.

  • printf()를 사용하여 반지름과 높이에 대한 프롬프트를 표시합니다.
  • scanf()를 사용하여 반지름과 높이에 대한 부동소수점 값을 읽습니다.
  • %.2f는 출력을 소수점 둘째 자리까지 표시하도록 형식을 지정합니다.

프로그램을 컴파일하고 실행합니다.

gcc cylinder_surface_area.c -o cylinder_surface_area
./cylinder_surface_area

예시 출력:

Enter the radius of the cylinder: 5
Enter the height of the cylinder: 10
Radius: 5.00
Height: 10.00

표면적 계산 = 2PIr*(r+h)

이 단계에서는 공식 Surface Area = 2PIr*(r+h) 를 사용하여 원기둥의 표면적을 계산하는 이전 프로그램을 수정합니다.

기존 파일을 열고 코드를 업데이트합니다.

cd ~/project
nano cylinder_surface_area.c

이전 코드를 다음과 같이 바꿉니다.

#include <stdio.h>
#define PI 3.14159

int main() {
    float radius, height, surface_area;

    // 사용자에게 반지름 입력 요청
    printf("Enter the radius of the cylinder: ");
    scanf("%f", &radius);

    // 사용자에게 높이 입력 요청
    printf("Enter the height of the cylinder: ");
    scanf("%f", &height);

    // 표면적 계산
    surface_area = 2 * PI * radius * (radius + height);

    // 결과 출력
    printf("Radius: %.2f\n", radius);
    printf("Height: %.2f\n", height);
    printf("Surface Area: %.2f\n", surface_area);

    return 0;
}

변경 사항을 살펴보겠습니다.

  • π에 대한 상수를 정의하기 위해 #define PI 3.14159를 추가했습니다.
  • 계산된 결과를 저장하기 위해 surface_area 변수를 만들었습니다.
  • 공식: Surface Area = 2PIr*(r+h) 를 사용했습니다.
  • 계산된 표면적을 표시하기 위한 출력 문을 추가했습니다.

프로그램을 컴파일하고 실행합니다.

gcc cylinder_surface_area.c -o cylinder_surface_area
./cylinder_surface_area

예시 출력:

Enter the radius of the cylinder: 5
Enter the height of the cylinder: 10
Radius: 5.00
Height: 10.00
Surface Area: 314.16

표면적 출력

이 마지막 단계에서는 원기둥의 표면적을 더욱 사용자 친화적인 출력 형식으로 제공하도록 프로그램을 개선합니다.

기존 파일을 열고 최종 수정을 수행합니다.

cd ~/project
nano cylinder_surface_area.c

형식화된 출력으로 코드를 업데이트합니다.

#include <stdio.h>
#define PI 3.14159

int main() {
    float radius, height, surface_area;

    // 사용자에게 반지름 입력 요청
    printf("Cylinder Surface Area Calculator\n");
    printf("--------------------------------\n");
    printf("Enter the radius of the cylinder: ");
    scanf("%f", &radius);

    // 사용자에게 높이 입력 요청
    printf("Enter the height of the cylinder: ");
    scanf("%f", &height);

    // 표면적 계산
    surface_area = 2 * PI * radius * (radius + height);

    // 형식화된 결과 출력
    printf("\nCalculation Results:\n");
    printf("--------------------------------\n");
    printf("반지름:        %.2f 단위\n", radius);
    printf("높이:        %.2f 단위\n", height);
    printf("표면적:  %.2f 제곱 단위\n", surface_area);

    return 0;
}

프로그램을 컴파일하고 실행합니다.

gcc cylinder_surface_area.c -o cylinder_surface_area
./cylinder_surface_area

예시 출력:

Cylinder Surface Area Calculator
--------------------------------
Enter the radius of the cylinder: 5
Enter the height of the cylinder: 10

Calculation Results:
--------------------------------
반지름:        5.00 단위
높이:        10.00 단위
표면적:  314.16 제곱 단위

주요 개선 사항:

  • 계산기 제목 추가
  • 가독성을 높이기 위해 구분자 포함
  • 출력에 단위 추가
  • 명확한 레이블로 결과 형식화

요약

이 실험에서는 C 프로그래밍을 사용하여 원기둥의 반지름과 높이를 읽고, 공식 Surface Area = 2PIr*(r+h) 를 사용하여 원기둥의 표면적을 계산하는 방법을 배웠습니다. 프로그램은 사용자에게 반지름과 높이를 입력하도록 요청하고, 그런 다음 표면적을 계산하여 표시합니다.

이 실험에서 다룬 주요 단계는 다음과 같습니다. 1) 사용자로부터 반지름과 높이를 읽고, 2) 제공된 공식을 사용하여 표면적을 계산하는 것입니다.