C# How To Add Two Numbers
Add Two Numbers
Learn how to add two numbers in C#:
Example
int x = 5;
int y = 6;
int sum = x + y;
Console.WriteLine(sum); // Print the sum of x + y
Add three numbers
In C#, you can add three (or more) numbers:
Example
// This example demonstrates adding three numbers in C#
// Declaration of variables
int num1 = 10;
int num2 = 20;
int num3 = 30;
// Adding three numbers
int sum = num1 + num2 + num3;
// Outputting the result
Console.WriteLine("The sum of three numbers: " + sum);
Виведення буде:
The sum of three numbers: 60
Explanation of the example:
- In this example, we declare three variables of type int (integer) named num1, num2, and num3.
- Next, we add the values of these three variables using the + operator.
- The result of the addition is stored in the variable sum.
- Finally, we use the Console.WriteLine() method to output the value of the sum variable to the console.
Variations:
- You can change the values of the variables num1, num2 and num3 to any other numbers.
- You can add more numbers to the sum by using the additional operators +.
- You can use variables of other types, such as float (floating-point number) or double (double-precision number).
- This example is just a basic example of adding numbers in C#. There are many other ways to accomplish this task.