C 言語で球の体積を求める

CCBeginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、C 言語を使用して球の体積を求める方法を学びます。この実験は、主に2つのステップで構成されています。まず球の半径を読み取り、次に公式 V = (4.0/3.0) _ π _ r³ を使用して体積を計算します。ユーザーに半径の入力を促し、体積を計算し、結果を表示する簡単なプログラムを作成します。この実験が終わる頃には、幾何学的な計算とそれを C 言語で実装する方法について、より深い理解が得られるでしょう。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/BasicsGroup(["Basics"]) c(("C")) -.-> c/FunctionsGroup(["Functions"]) c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c/BasicsGroup -.-> c/variables("Variables") c/BasicsGroup -.-> c/constants("Constants") c/FunctionsGroup -.-> c/math_functions("Math Functions") c/UserInteractionGroup -.-> c/user_input("User Input") c/UserInteractionGroup -.-> c/output("Output") subgraph Lab Skills c/variables -.-> lab-435173{{"C 言語で球の体積を求める"}} c/constants -.-> lab-435173{{"C 言語で球の体積を求める"}} c/math_functions -.-> lab-435173{{"C 言語で球の体積を求める"}} c/user_input -.-> lab-435173{{"C 言語で球の体積を求める"}} c/output -.-> lab-435173{{"C 言語で球の体積を求める"}} end

半径を読み取る

このステップでは、C 言語を使用して球の半径を読み取る方法を学びます。ユーザーに半径の入力を促し、それを変数に格納する簡単なプログラムを作成します。

まず、~/project ディレクトリに新しい C ファイルを作成しましょう。

cd ~/project
nano sphere_volume.c

では、半径を読み取るコードを書きましょう。

#include <stdio.h>

int main() {
    // Declare a variable to store the radius
    double radius;

    // Prompt the user to enter the radius
    printf("Enter the radius of the sphere: ");

    // Read the radius from user input
    scanf("%lf", &radius);

    // Print the entered radius to confirm
    printf("Radius entered: %.2f\n", radius);

    return 0;
}

出力例:

Enter the radius of the sphere: 5.5
Radius entered: 5.50

コードを分解してみましょう。

  • double radius; は、半径を浮動小数点数として格納するための変数を宣言します。
  • printf() は、ユーザーに半径の入力を促すプロンプトを表示します。
  • scanf() は、ユーザーの入力を読み取り、radius 変数に格納します。
  • %.2f は、出力を小数点以下2桁で表示するようにフォーマットします。

プログラムをコンパイルします。

gcc sphere_volume.c -o sphere_volume

出力例:

labex@ubuntu:~/project$ gcc sphere_volume.c -o sphere_volume

これでプログラムを実行できます。

./sphere_volume

体積 = (4.0/3.0)PIr³ を計算する

このステップでは、数学的な公式 V = (4.0/3.0) _ π _ r³ を使用して球の体積を計算する方法を学びます。前のプログラムを修正して、体積の計算を含めます。

既存のファイルを開き、コードを更新します。

cd ~/project
nano sphere_volume.c

前のコードを次のコードに置き換えます。

#include <stdio.h>
#include <math.h>

int main() {
    // Declare variables
    double radius, volume;

    // Constant for PI
    const double PI = 3.14159265358979323846;

    // Prompt the user to enter the radius
    printf("Enter the radius of the sphere: ");

    // Read the radius from user input
    scanf("%lf", &radius);

    // Calculate the volume using the sphere volume formula
    volume = (4.0 / 3.0) * PI * pow(radius, 3);

    // Print the radius and calculated volume
    printf("Radius: %.2f\n", radius);
    printf("Volume of the sphere: %.2f\n", volume);

    return 0;
}

更新したプログラムをコンパイルします。

gcc sphere_volume.c -o sphere_volume -lm

出力例:

labex@ubuntu:~/project$ gcc sphere_volume.c -o sphere_volume -lm

pow() 関数に必要な数学ライブラリをリンクする -lm フラグに注意してください。

プログラムを実行します。

./sphere_volume

出力例:

Enter the radius of the sphere: 5.5
Radius: 5.50
Volume of the sphere: 696.46

主な変更点を分解してみましょう。

  • pow() 関数を使用するために #include <math.h> を追加しました。
  • 正確な計算のために定数 PI を定義しました。
  • 公式 volume = (4.0 / 3.0) * PI * pow(radius, 3) を使用しました。
  • pow(radius, 3) は r³ を計算します。
  • 半径と計算された体積の両方を出力しました。

体積を出力する

この最後のステップでは、体積計算の結果をより見やすく使いやすくするために、出力の書式を改善します。

既存のファイルを開きます。

cd ~/project
nano sphere_volume.c

書式を改善したコードに更新します。

#include <stdio.h>
#include <math.h>

int main() {
    // Declare variables
    double radius, volume;

    // Constant for PI
    const double PI = 3.14159265358979323846;

    // Prompt the user to enter the radius
    printf("Sphere Volume Calculator\n");
    printf("------------------------\n");
    printf("Enter the radius of the sphere: ");

    // Read the radius from user input
    scanf("%lf", &radius);

    // Calculate the volume using the sphere volume formula
    volume = (4.0 / 3.0) * PI * pow(radius, 3);

    // Print formatted output
    printf("\nCalculation Results:\n");
    printf("-------------------\n");
    printf("Radius:        %.2f units\n", radius);
    printf("Volume:        %.2f cubic units\n", volume);

    // Additional descriptive output
    printf("\nNote: Volume is calculated using the formula V = (4/3) * π * r³\n");

    return 0;
}

プログラムをコンパイルします。

gcc sphere_volume.c -o sphere_volume -lm

プログラムを実行したときの出力例:

Sphere Volume Calculator
------------------------
Enter the radius of the sphere: 5.5

Calculation Results:
-------------------
Radius:        5.50 units
Volume:        696.46 cubic units

Note: Volume is calculated using the formula V = (4/3) * π * r³

主な改善点:

  • 説明的なヘッダーを追加しました。
  • 単位付きで出力を書式設定しました。
  • 計算公式に関する注釈を追加しました。
  • 結果の読みやすさを向上させました。

動作を確認するためにプログラムを実行します。

./sphere_volume

まとめ

この実験では、ユーザーの入力から球の半径を読み取り、公式 V = (4.0/3.0) _ π _ r³ を使用してその体積を計算する方法を学びます。まず、半径を格納するための変数を宣言し、ユーザーに値の入力を促します。次に、数学的な公式と π の定数値を使用して球の体積を計算します。最後に、計算された体積をコンソールに出力します。