Table of Contents
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
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:
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.