C# PROGRAM TO ADD TWO INTEGERS USING METHOD
C# PROGRAM TO ADD TWO INTEGERS USING METHOD :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
namespace CsharpPrograms { class Program { static void Main(string[] args) { int x,y,result=0; Console.WriteLine(">>> PROGRAM TO ADD TWO NUMBERS WITHOUT USING THIRD VARIABLE <<< "); Console.Write("\n Enter the first number to be added: "); x=Convert.ToInt32(Console.ReadLine()); // taking x value from keyboard Console.Write("\n Enter the second number to be added: "); y = Convert.ToInt32(Console.ReadLine()); // taking y value from keyboard result= Add(x,y); //calling function Console.WriteLine("\n The sum of two numbers is: {0} ",result); /*printing the sum.*/ Console.ReadLine(); } static public int Add(int a, int b) { int result = a + b; return result; } } } |
Sample Output:
Explanation :
In the above program the function static public int Add(int a,int b) , we are declaring function as Static . Because, c# is a Object Oriented Programming Language. And in OOP language, a function can be called using object of a class. And if we want to call function without creating object, we have to declare it as static.
For C Program : C Program to Add Two Numbers Using Function