HOWTO · Csharp
C# でのパーセンテージ計算
この記事では、C# でパーセンテージを計算する方法を学習します。
このページの内容
この記事では、C# プログラミング言語でパーセンテージを計算する方法を説明します。
C# を使用してパーセンテージを計算するプログラムを作成する
まず、次のライブラリをインポートする必要があります。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
次に、sub1、sub2、sub3、sub4 という名前の 4つの double 型変数を作成します。すべての科目の合計点数を保存する total という変数もあります。
また、per という名前の double タイプのパーセンテージ変数を作成します。
double sub1, sub2, sub3, sub4, total;
double per;
これで、提供された式を使用してパーセンテージを計算できます。
per = total / 4; // Here "total" holds the total marks of all subjects, and "4" is the total number
// of subjects.
完全なソースコード:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class PercentageCalculation {
static void Main(string[] args) {
double sub1, sub2, sub3, sub4, total;
double per;
Console.Write("Enter marks of subjects to find out the percentage. \n");
Console.Write("Input the marks of Subject1: ");
sub1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks of Subject2: ");
sub2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks of Subject3: ");
sub3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks of Subject4: ");
sub4 = Convert.ToInt32(Console.ReadLine());
total = sub1 + sub2 + sub3 + sub4;
per = total / 4;
Console.Write("Percentage = {0}%\n", per);
}
}
出力:
Enter marks of subjects to find out the percentage.
Input the marks of Subject1: 34
Input the marks of Subject2: 45
Input the marks of Subject3: 56
Input the marks of Subject4: 45
Percentage = 45%