Add Two Numbers
Learn how to add two numbers in C#.
C# How To: Add Two Numbers
Learn how to add two numbers in C#.
You can add numbers using the + operator.
Examples
Add Two Numbers
Adding two integers.
using System;
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 6;
int sum = x + y;
Console.WriteLine(sum); // Print the sum of x + y
}
}Add with User Input
Adding numbers from user input.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter another number:");
int y = Convert.ToInt32(Console.ReadLine());
int sum = x + y;
Console.WriteLine("Sum is: " + sum);
}
}