C 言語で直方体の体積を求める

CBeginner
オンラインで実践に進む

はじめに

この実験では、C 言語を使って直方体の体積を計算する方法を学びます。この実験では、ユーザーから直方体の寸法(長さ、幅、高さ)を読み取り、次に「体積 = 長さ × 幅 × 高さ」という公式を使って体積を計算する手順を段階的に案内します。最後に、計算された体積をコンソールに出力します。

この実験では、幾何学的な計算の基本概念をカバーし、それらを実践的な C 言語の演習にどのように適用するかを示します。

長さ、幅、高さの読み取り

このステップでは、C 言語を使って直方体の寸法を読み取る方法を学びます。ユーザーから長さ、幅、高さを入力として受け取る簡単なプログラムを作成します。

まず、プロジェクトディレクトリに新しい C ファイルを作成しましょう。

cd ~/project
nano volume_cuboid.c

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

#include <stdio.h>

int main() {
    // 寸法を格納する変数を宣言する
    float length, width, height;

    // ユーザーに長さの入力を促す
    printf("Enter the length of the cuboid: ");
    scanf("%f", &length);

    // ユーザーに幅の入力を促す
    printf("Enter the width of the cuboid: ");
    scanf("%f", &width);

    // ユーザーに高さの入力を促す
    printf("Enter the height of the cuboid: ");
    scanf("%f", &height);

    // 入力された寸法を表示する
    printf("Dimensions entered:\n");
    printf("Length: %.2f\n", length);
    printf("Width: %.2f\n", width);
    printf("Height: %.2f\n", height);

    return 0;
}

プログラムをコンパイルして実行しましょう。

gcc volume_cuboid.c -o volume_cuboid
./volume_cuboid

出力例:

Enter the length of the cuboid: 5
Enter the width of the cuboid: 3
Enter the height of the cuboid: 2
Dimensions entered:
Length: 5.00
Width: 3.00
Height: 2.00

体積 = 長さ × 幅 × 高さ の計算

このステップでは、前の C プログラムを修正して、「体積 = 長さ × 幅 × 高さ」という公式を使って直方体の体積を計算します。

既存のファイルを編集しましょう。

cd ~/project
nano volume_cuboid.c

体積計算を含むようにプログラムを更新します。

#include <stdio.h>

int main() {
    // 寸法と体積を格納する変数を宣言する
    float length, width, height, volume;

    // ユーザーに長さの入力を促す
    printf("Enter the length of the cuboid: ");
    scanf("%f", &length);

    // ユーザーに幅の入力を促す
    printf("Enter the width of the cuboid: ");
    scanf("%f", &width);

    // ユーザーに高さの入力を促す
    printf("Enter the height of the cuboid: ");
    scanf("%f", &height);

    // 体積を計算する
    volume = length * width * height;

    // 寸法と計算された体積を表示する
    printf("Dimensions entered:\n");
    printf("Length: %.2f\n", length);
    printf("Width: %.2f\n", width);
    printf("Height: %.2f\n", height);
    printf("Volume of the cuboid: %.2f cubic units\n", volume);

    return 0;
}

更新されたプログラムをコンパイルして実行しましょう。

gcc volume_cuboid.c -o volume_cuboid
./volume_cuboid

出力例:

Enter the length of the cuboid: 5
Enter the width of the cuboid: 3
Enter the height of the cuboid: 2
Dimensions entered:
Length: 5.00
Width: 3.00
Height: 2.00
Volume of the cuboid: 30.00 cubic units

体積の出力

この最後のステップでは、体積が明確に出力され、読みやすい形式に整形されるようにします。前のプログラムを修正して、出力の表示を改善します。

最後に一度ファイルを編集しましょう。

cd ~/project
nano volume_cuboid.c

体積の出力を改善したプログラムに更新します。

#include <stdio.h>

int main() {
    // 寸法と体積を格納する変数を宣言する
    float length, width, height, volume;

    // ユーザーに長さの入力を促す
    printf("Cuboid Volume Calculator\n");
    printf("------------------------\n");
    printf("Enter the length of the cuboid: ");
    scanf("%f", &length);

    // ユーザーに幅の入力を促す
    printf("Enter the width of the cuboid: ");
    scanf("%f", &width);

    // ユーザーに高さの入力を促す
    printf("Enter the height of the cuboid: ");
    scanf("%f", &height);

    // 体積を計算する
    volume = length * width * height;

    // 詳細な体積情報を出力する
    printf("\nCalculation Results:\n");
    printf("-------------------\n");
    printf("Length:  %.2f units\n", length);
    printf("Width:   %.2f units\n", width);
    printf("Height:  %.2f units\n", height);
    printf("Volume:  %.2f cubic units\n", volume);

    return 0;
}

プログラムの最終バージョンをコンパイルして実行しましょう。

gcc volume_cuboid.c -o volume_cuboid
./volume_cuboid

出力例:

Cuboid Volume Calculator
------------------------
Enter the length of the cuboid: 5
Enter the width of the cuboid: 3
Enter the height of the cuboid: 2

Calculation Results:
-------------------
Length:  5.00 units
Width:   3.00 units
Height:  2.00 units
Volume:  30.00 cubic units

まとめ

この実験では、C 言語を使って直方体の寸法(長さ、幅、高さ)を読み取る方法を学びました。その後、「体積 = 長さ × 幅 × 高さ」という公式を使って直方体の体積を計算しました。最後に、計算された体積をコンソールに出力しました。

主な手順は、ユーザーから寸法を読み取り、体積を計算し、結果を表示することでした。この実験は、C 言語での変数の扱いと算術演算の基本的な導入を提供します。