addition

Sum of two floating-point numbers – Example program in C#

Sum of two floating-point numbers – Example program in C#

In this tutorial, we will discuss the title of  Sum of two floating-point numbers – Example program in C#

In this topic, we are going to learn how  to add two floating-point numbers in C# programming language

Sum of two floating-point numbers –

Addition of two floating-point numbers – program in C#

Sum of two floating -point numbers using variables

Program 1

//find sum of two floating point numbers 
using System;

public class SumOfTwoFloatNum
{
    public static void Main(string[] args)
    {
        float n1=5.8f;
         float n2=6.9f;
         
         Console.Write("The sum of two floats: "+n1+"+"+n2+"=");
         float result=n1+n2;
         Console.WriteLine(result);
    }
}

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

The sum of two floats: 5.8+6.9=12.7

 

Sum of two numbers using user input

Program 2

In this program, the user asked to enter two floating-point values. these two floating-point numbers are stored in variables n1 and n2 respectively. Then two numbers are added using + operator and the result is stored in the result variable

//Calculate sum of two floating point numbers 
using System;

public class SumOfFloat
{
    public static void Main(string[] args)
    {
        Console.WriteLine ("Enter a floating point number for n1");
        float n1=float.Parse(Console.ReadLine());
        Console.WriteLine ("Enter a floating point number for n2");
         float n2=float.Parse(Console.ReadLine());
         
         Console.Write("The total of two floats: "+n1+"+"+n2+"=");
         float result=n1+n2;
         Console.WriteLine(result);
    }
}

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

Enter a floating point number for n1
2.45
Enter a floating point number for n2
4.76
The total of two floats: 2.45+4.76=7.21

 

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

Then the result is stored in another variable result.

Finally, the result is displayed on the screen 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 Find sum of two floating-point numbers in C#
C# program to Subtract two numbers using function - Example program
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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago