In this example, we will learn how to find sum of three numbers using functions in C#.
Write a program to explain method in C#.
Create a static function Add() that accept three number from user and returns sum of the three numbers.
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Program { public static int Add(int n1,int n2,int n3) { int total = (n1 + n2 + n3); return total; } static void Main(string[] args) { int num1, num2,num3,sum; Console.Write("Number 1: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Number 2: "); num2 = Convert.ToInt32(Console.ReadLine()); Console.Write("Number 3: "); num3 = Convert.ToInt32(Console.ReadLine()); sum = Add(num1, num2,num3); Console.WriteLine("The Sum of three numbers is "+sum); Console.ReadKey(); } } |
Output: