Arithmetic Caculation

Sum of two floating point numbers in C#

Sum of two floating point numbers in C#

In this article, we will discuss the concept of Sum of two floating point numbers in C#

In this post, we are going to learn how to write a program to  calculate sum of two floating point  numbers in C# language

Code to sum of two floating point numbers in C#

Add two floating -point numbers in C#

In this code, we are going to learn how to write a program to add two floating -point numbers using initialized variables in C# programming language

Program 1

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

public class SumOfFloatNum
{
    public static void Main(string[] args)
    {
        float num1=45.8f;
         float num2=56.9f;//declare and initialize variables
         
         Console.Write("The total of two floats: "+num1+"+"+num2+"=");
         float result=num1+num2;//Calculate addition of two numbers
         Console.WriteLine(result);//display result
    }
}

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

The total of two floats: 45.8+56.9=102.7

 

Add two floating -point numbers 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) in C# programming language

Program 2

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

public class SumOfFloatNum
{
    public static void Main(string[] args)
    {
        Console.WriteLine ("Enter float number to fNo");//ask input from user for fNo
        float fNo=float.Parse(Console.ReadLine());//input value by user to fNo
        Console.WriteLine ("Enter second float number to lNo");//ask input from user for lNo
         float lNo=float.Parse(Console.ReadLine());//input value by user to lNo
         
         Console.Write("The total of two floats: "+fNo+"+"+lNo+"=");
         float result=fNo+lNo;//calculate sum
         Console.WriteLine(result);
    }//display result on the screen
}

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

Enter float number to fNo
76.897
Enter second float number to lNo
45.984
The total of two floats: 76.897+45.984=122.881

 

In this code, the user asked to enter two floating-point numbers and the given numbers are stored in variables fNo and lNo respectively.

Then these two numbers are added using the plus(+) operator and the result is stored in the result variable.

Finally the display statement is used to print the addition of these floating-point numbers.

 

Suggested for you

Sum of two integers in C#

Sum of two integers using function in C#

 

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

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.