In this example, we will learn how to calculate sum of two numbers using functions in C#.
Add two numbers with a function in C# Console Application.
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Program { public static int Sum(int n1,int n2) { int total = n1 + n2; return total; } static void Main(string[] args) { int num1, num2,sum; Console.Write("Number 1: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Number 2: "); num2 = Convert.ToInt32(Console.ReadLine()); sum = Sum(num1, num2); Console.WriteLine("The sum of two numbers is "+sum); Console.ReadKey(); } } |
Output: