Sure! Adding two numbers in C# is a basic operation. You can do it in various ways, but let's start with a simple console application that prompts the user to enter two numbers and then adds them together.
Here's a straightforward example:
Example
using System;
class Program
{
static void Main(string[] args)
{
// Prompt the user to enter the first number
Console.Write("Enter the first number: ");
string input1 = Console.ReadLine();
// Convert the user input to an integer
int number1 = int.Parse(input1);
// Prompt the user to enter the second number
Console.Write("Enter the second number: ");
string input2 = Console.ReadLine();
// Convert the user input to an integer
int number2 = int.Parse(input2);
// Add the two numbers together
int sum = number1 + number2;
// Display the result
Console.WriteLine("The sum of " + number1 + " and " + number2 + " is: " + sum);
}
}
In this example:
Console.ReadLine()
method to read input from the user, which is in the form of a string.int.Parse()
to convert the string input to integers so that we can perform arithmetic operations on them.+
operator and store the result in a variable called sum
.Console.WriteLine()
to display the sum to the user.When you run this program, it will prompt you to enter two numbers, add them together, and display the result. It's a simple and basic example to demonstrate adding two numbers in C#.