HOWTO · Csharp

Prozentrechnung in C#

In diesem Artikel erfahren Sie, wie Sie Prozentsätze in C# berechnen.

Auf dieser Seite

In diesem Artikel wird gezeigt, wie Prozentsätze in der Programmiersprache C# berechnet werden.

Erstellen Sie ein Programm, das den Prozentsatz mit C# berechnet

Zunächst müssen wir die folgenden Bibliotheken importieren.

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

Dann konstruieren Sie vier Variablen vom Typ double mit den Namen sub1, sub2, sub3 und sub4. Es wird auch eine Variable namens total geben, die die Gesamtpunktzahl für alle Fächer speichert.

Erstellen Sie außerdem eine Prozentvariable vom Typ double mit dem Namen per.

double sub1, sub2, sub3, sub4, total;
double per;

Wir können jetzt den Prozentsatz mit der bereitgestellten Formel berechnen.

per = total / 4;  // Here "total" holds the total marks of all subjects, and "4" is the total number
                  // of subjects.

Vollständiger Quellcode:

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);
  }
}

Ausgabe:

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%