addition

Function to calculate the sum of two numbers in C#

Function to calculate the sum of two numbers in C#

In this tutorial, we will discuss the Function to calculate the sum of two numbers in C#

In this topic, we are going to learn how  to write a program to add two numbers using function in C# programming language

Sum of two numbers

Function to calculate the sum of two numbers

Sum of two numbers using variables

Program 1

//Write a program calculate sum of two numbers using function
using System; 
public class Sum_Two_Num_Fun { 
    public static int sum_Num(int num1, int num2) { 
        int total; 
        total=num1+num2; 
        return total; 
    } 
        
        public static void Main() {
           int num1=45;
        int num2=56;
            Console.WriteLine("The sum of {0} and {1} is: {2}: ",num1,num2,sum_Num(num1,num2)); 
        } 
    
}

When the above code is executed, it produces the following result

The sum of 45 and 56 is: 101:

 

Sum of two numbers using user input

Program 2

In this program, the user asked to enter two integer values. these two integers are stored in variables n1 and n2 respectively then two numbers are added using + operator and the result is stored in the total variable

// Write a program calculate sum of two numbers using function
    using System;

public class Sum_Two_Num_Fun
{
     public static int sum_Num(int num1, int num2)
     { int total;
     total=num1+num2;
     return total;
     }
    public static void Main()
    {
        Console.Write("Enter number for n1: ");
        int n1=Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter number for n2: ");
        int n2=Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("The sum of {0} and {1} is: {2}: ",n1,n2,sum_Num(n1,n2));
    }
}

When the above code is executed, it produces the following result

Enter number for n1: 55
Enter number for n2: 66
The sum of 55 and 66 is: 121:

 

In the above program, two integer values 55,66 (get input from the user using Scanner class) are stored in n1 and n2. Then given two numbers are added using the plus(+) operator.

Then the result is stored in another variable total inside the sumNum() function.

Finally, the result is displayed on the screen using display statements (Calling the function) in C#

 

Suggested for you

Operator in Java language

Data types and variable in Java language

 

Similar post

Sum of two numbers in C language

Sum of two numbers in C++ language

Sum of two numbers in Python language

Sum of two numbers in Java language without + operator

Sum of two numbers in Java language using bitwise operator

Sum of two numbers in C language without + operator

Sum of two numbers in C language using bitwise operator

 

Calculate addition of two numbers using method in Java language

Calculate addition of two numbers using function in C language

Calculate addition of two numbers using function in C++ language

Calculate addition of two numbers using function in Python language

Calculate sum of two numbers - Example program in C#
Function to Find sum of two floating-point numbers in C#
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago