In this example, we will learn how to find average of two numbers using functions in C#.
Write a program to explain method in C#.
Create a static function Average() that accept two number from user and returns average of the two numbers.
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 double Average(double n1,double n2) { int avrg = n1 + n2; return avrg; } static void Main(string[] args) { int num1, num2,avg; Console.Write("Number 1: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Number 2: "); num2 = Convert.ToInt32(Console.ReadLine()); avg = Average(num1, num2); Console.WriteLine("The average of two numbers is "+avg); Console.ReadKey(); } } |
Output: