Category > CSHARP || Published on : Tuesday, March 7, 2023 || Views: 13 || C# programming arithmetic operations math functions formatting output.

If you are new to C# programming, you may be wondering how to work with numbers in the language. This article will cover the basics of working with numbers in C# including basic arithmetic operations, using built-in math functions, and formatting output.

Working with numbers is a fundamental part of programming in any language, and C# is no exception. In this article, we will cover the basics of working with numbers in C# including basic arithmetic operations, using built-in math functions, and formatting output.

**Basic Arithmetic Operations**

C# supports all the basic arithmetic operations you would expect, including **addition, subtraction, multiplication, and division**. The operators for these operations are as follows:

- Addition: +
- Subtraction: -
- Multiplication: *
- Division: /

**Here's an example of using these operators in C#:**

int a = 10; int b = 5; int sum = a + b; // sum = 15 int difference = a - b; // difference = 5 int product = a * b; // product = 50 int quotient = a / b; // quotient = 2

Note that when dividing two integers, C# will perform integer division and truncate any remainder. If you want to perform floating-point division, you can cast one or both operands to a floating-point type:

int a = 10; int b = 3; float quotient = (float)a / b; // quotient = 3.33333

**Built-in Math Functions**

C# provides a wide range of built-in math functions that you can use to perform more complex operations. Here are a few examples:

**Math.Abs**: returns the absolute value of a number**Math.Sqrt**: returns the square root of a number**Math.Pow**: raises a number to a specified power**Math.Round**: rounds a number to the nearest integer or specified number of decimal places

**Here's an example of using these functions in C#:**

int a = -10; double b = 3.14159; int absA = Math.Abs(a); // absA = 10 double sqrtB = Math.Sqrt(b); // sqrtB = 1.77245385091 double powB = Math.Pow(b, 2); // powB = 9.86960440109 double roundedB = Math.Round(b, 2); // roundedB = 3.14

**Formatting Output**

When working with numbers, you may want to format the output to make it easier to read. C# provides a variety of formatting options that you can use to control how numbers are displayed. Here are a few examples:

**N**: displays a number with a thousands separator**C**: displays a number as currency**F**: displays a number with a fixed number of decimal places

**Here's an example of using these formatting options in C#:**

int a = 10000; double b = 3.14159; string formattedA = a.ToString("N0"); // formattedA = "10,000" string formattedB1 = b.ToString("C"); // formattedB1 = "$3.14" string formattedB2 = b.ToString("F2"); // formattedB2 = "3.14"

In the above code snippet, we are using the ToString method to format the numbers as strings with the specified format string.

Conclusion

In this article, we covered the basics of working with numbers in C# including basic arithmetic operations, using built-in math functions, and formatting output. By mastering these fundamentals, you will be well on your way to writing more sophisticated C# programs that involve complex mathematical operations.

- Truncate Strings with Complete Words without chopping words half way In C#
- Load jQuery .js file even if the Google CDN is down
- Introduction to ASP.NET MVC
- How to Send Bulk Email in Asp.net with Example using C#
- Get QueryString values using JavaScript
- How can we call a C# Method from ASPX Page or Code Behind page
- AutoComplete Feature in winform application using C Sharp
- how can we convert negative a number to positive number in SQL Server 2005/2008.
- How to validate or check whether checkbox is checked or not using ASP.NET and jQuery with example.
- Set meta tag in asp.net programmaticaly for creating seo friendly websites
- Get todays Day Name SQL server
- How to Convert Numbers to Words String in ASP.NET using C#
- How can we Insert, Update, Delete Records in ASP.NET Server Control ListView in ASP.NET with C#
- How to use AJAX Timer Control in ASP.NET using C# with Example and Source Codes
- Using jQuery How We Can Limit Number of Characters in Textarea or Textbox
- How to develop a Simple Chat Application in C# Using SignalR
- How can we copy data From one Table to another Table in MS-SQL Server
- How to play YouTube videos in ASP.NET Web Application
- What is PHP?
- In MS-SQL Server How to get tables columns with increment value with SQL Query Example

Terms & Conditions | Privacy Policy | Contact

© www.sourcecodehub.com 2011-2022