## Introduction

The C# programming language provides several ways to calculate percentages. In this article, we will explore different methods to calculate percentages in C# with examples.

## Method 1: Using Arithmetic Operators

One way to calculate percentages in C# is by using arithmetic operators. Here’s an example:

```
int total = 100;
int percentage = 20;
int result = (percentage * total) / 100;
Console.WriteLine("Result: " + result);
```

In this example, we have a total value of 100 and a percentage value of 20. We calculate the result by multiplying the percentage with the total and then dividing it by 100. The result is then printed to the console.

## Method 2: Using the Math Class

Another way to calculate percentages in C# is by using the Math class. The Math class provides a method called `Round`

that can be used to round off decimal values. Here’s an example:

```
double total = 100.0;
double percentage = 20.0;
double result = Math.Round((percentage / 100.0) * total, 2);
Console.WriteLine("Result: " + result);
```

In this example, we have total and percentage values as doubles. We divide the percentage by 100 and multiply it with the total. The `Round`

method is used to round off the result to two decimal places. The rounded result is then printed to the console.

## Method 3: Using the Percentage Format String

C# provides a format string for percentages that can be used to calculate and format percentages. Here’s an example:

```
double total = 100.0;
double percentage = 20.0;
double result = (percentage / 100.0) * total;
Console.WriteLine("Result: {0:P}", result);
```

In this example, we calculate the result by dividing the percentage by 100 and multiplying it with the total. The `{0:P}`

format string is used to format the result as a percentage. The formatted result is then printed to the console.

## Conclusion

In this article, we explored different methods to calculate percentages in C#. Whether you prefer using arithmetic operators, the Math class, or the percentage format string, C# provides multiple options to perform percentage calculations. Choose the method that best suits your requirements and implement it in your C# programs.