basic

Sum of two floating point numbers using function in c#

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 in C#

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

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

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,…

1 year 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…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

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

1 year 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…

1 year ago

This website uses cookies.