addition

Function to Find sum of two floating-point numbers in C#

Function to Find sum of two floating-point numbers in C#

In this tutorial, we will discuss the concept of  Function to Find sum of two floating-point numbers in C#

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

Sum of two floating-point numbers

sum of two floating-point numbers in C#

sum of two floating-point numbers

Program 1

//Write a program calculate sum of two numbers using function
using System; 
public class Sum_Two_Num_Fun { 
    public static float sum_Num(float num1, float num2) { //function definition
        float total; 
        total=num1+num2; //calculate addition of two numbers
        return total; //return value
        
    } 
    public static void Main() {
        float num1=45.8f; //declare and initialize float variable
        float num2=56.9f; 
        Console.WriteLine("The sum of {0} and {1} is: {2}: ",num1,num2,sum_Num(num1,num2)); } }//display result

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

The sum of 45.8 and 56.9 is: 102.7:

 

Sum of two floating-point numbers using user input

Program 2

In this program, the user asked to enter two floating-point values. these two floating point values 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 float sum_Num(float num1, float num2) { 
        float total; 
        total=num1+num2; 
        return total; 
        
    } public static void Main() { 
        Console.Write("Enter number for n1: "); 
        float n1=Convert.ToSingle(Console.ReadLine()); 
        Console.Write("Enter number for n2: "); 
        float n2=Convert.ToSingle(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: 34.67
Enter number for n2: 56.8
The sum of 34.67 and 56.8 is: 91.47:

 

In the above program, two floating point values 34.67,56.8 (get input from the user) are stored in n1 and n2. Then given two values are added using the plus(+) operator.

Then the result is stored in another variable total.

Finally, the result is displayed on the screen (Calling the function)using display statements 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

 

Function to calculate the sum of two numbers in C#
Sum of two floating-point numbers - Example program 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.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago