Arithmetic Caculation

Calculate sum of two numbers using function in C#

Calculate sum of two numbers using function in C#

In this article, we will discuss the concept of Calculate sum of two numbers using function in C#

In this post, we are going to learn how to write a program to  calculate sum of two numbers using function in C# language

Code to find sum of two numbers in C#

Add two numbers using function in C#

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

Program 1

//Write a program calculate sum of two numbers using function
using System; 
public class Sum_Two_Num_Function { 
    public static int 
    sum_Num(int fNum, int sNum) { //function definition with parameter
        int tot; 
        tot=fNum+sNum; //Find addition of two numbers
        return tot; //return tot to main method
        
    }
        public static void Main() { 
            int fNum=45; 
            int sNum=56; //variable declaration and initialization for numbers
            Console.WriteLine("The sum of {0} and {1} is: {2}: ",fNum,sNum,sum_Num(fNum,sNum)); } }
            //display total of two numbers

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

The sum of 450 and 560 is: 1010:

 

Add two numbers using function in C# – Entered by user

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

Program 2

// 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) { 
        //Function definition
        int total;//variable for calculate  total of two num
        total=num1+num2; //Calculate total
        return total; //return total
        
    } 
    public static void Main() { 
        Console.Write("Enter number for n1: ");//Ask input from the user 
        int n1=Convert.ToInt32(Console.ReadLine()); //reading the input
        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)); } }
        //Display addition of two numbers

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

Enter number for n1: 666
Enter number for n2: 777
The sum of 666 and 777 is: 1443:

 

In this code, the user asked to enter two integers and the given integers are stored in variables n1 and n2 respectively.

Then these two numbers are added using the plus(+) operator and the result is stored in the total variable.

Finally the display statement is used to print the addition of this numbers using calling the function

 

Suggested for you

Write a program to sum of prime numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

 

Write a program to sum of natural numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.