Table of Contents
Sum of two floating point numbers using function in c#
In this article, we will discuss the concept of sum of two floating point numbers using function in c#
In this post, we are going to learn how to write a program to add two numbers using user defined function in C# language
Code to Add 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 using initialized variables with function in C# programming language
Program 1
//Write a program calculate sum of two numbers using function using System; public class Sum_Two_Float_Num_Fun { public static float sum_Num(float num1, float num2) { //function definition float sum; sum=num1+num2; //calculate addition of two numbers return sum; //return value } public static void Main() { float n1=68.8f; //declare and initialize float variable float n2=53.7f; Console.WriteLine("The sum of {0} and {1} is: {2}: ",n1,n2,sum_Num(n1,n2)); } }//display result
When the above code is executed, it produces the following result
The sum of 68.8 and 53.7 is: 122.5:
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_Num_Fun { public static float sum_Num(float num1, float num2) { float sum; //declare a variable for total sum=num1+num2; //addition of two numbers return sum; //return sum to main method } public static void Main() { Console.Write("Enter number for fNum: "); float fNum=Convert.ToSingle(Console.ReadLine()); //input first number Console.Write("Enter number for lNum: "); float lNum=Convert.ToSingle(Console.ReadLine()); //input second number Console.WriteLine("The sum of {0} and {1} is: {2}: ",fNum,lNum,sum_Num(fNum,lNum)); //display total } }
When the above code is executed, it produces the following result
Enter number for fNum: 123.45 Enter number for lNum: 234.56 The sum of 123.45 and 234.56 is: 358.01:
In this code, the user asked to enter two floating-point value and the given values are stored in variables fNum and lNum respectively.
Then these two values are added using the plus(+) operator and the result is stored in the sum variable.
Finally the display statement is used to print the addition of this numbers by calling the function
Suggested for you
Sum of two integers using function in C#
Sum of two floating point numbers in C#
Write a program to add prime numbers 1 to n in Java language
Write a program to add prime numbers 1 to n in C language
Write a program to add prime numbers 1 to n in C++ language
Write a program to add 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