Search This Blog

This website completely moved to new domain. For latest content, visit www.programmingposts.com

C# PROGRAM TO ADD TWO INTEGERS USING FUNCTION


 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
28
29
30
using System;

namespace AddTwoNumsUsingFunc
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y, result = 0;
            Console.WriteLine("*** www.ProgrammingPosts.blogspot.com *** ");
            Console.WriteLine(">>>C# PROGRAM TO ADD TWO NUMBERS USING METHOD <<< ");
            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 = Sum(x, y);     //calling function

            Console.WriteLine("\n The sum of two numbers is: {0} ", result);    /*printing the sum.*/
            Console.ReadLine();
        }

        static public int Sum(int a, int b)
        {
            int result = a + b;
            return result;
        }   


    }
}

Sample Output :
Explanation : In the above program the method static public int Add(int a,int b) , we are declaring method Sum method as Static because Main method is static and we are calling Sum method in Main method without creating object. 
c# is a Object Oriented Programming Language. And in OOP language, a method can be called using object of a class. And if we want to call method without creating object, we have to declare it as static to Call it with its class name.
For C Program : C Program to Add Two Numbers Using Function

7 comments:

  1. This blog shares some excellent insights into the subject. Thank you for providing such useful content.workday hcm course

    ReplyDelete
  2. The article offers a helpful introduction to the topic. It is informative, well structured, and pleasant to read.ba analyst course online

    ReplyDelete
  3. Excellent content! Anyone looking to build a career in data engineering can benefit from IBM DataStage training and practical experience with real-time projects.datastage training



    ReplyDelete
  4. I found this article interesting because it explains the subject without making it too complicated.ui path learning

    ReplyDelete
  5. Thank you for sharing these ideas. The information is helpful for students who want to learn new skills.training uipath

    ReplyDelete