Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display Absolute value of a number in C#?
To find the absolute value of a number in C#, use the Math.Abs method.
Set numbers first −
int val1 = 77; int val2 = -88;
Now take two new variables and get the Absolute value of the above two numbers −
int abs1 = Math.Abs(val1); int abs2 = Math.Abs(val2);
Let us see the complete code to display Absolute value of a number −
Example
using System;
class Program {
static void Main() {
int val1 = 77;
int val2 = -88;
Console.WriteLine("Before...");
Console.WriteLine(val1);
Console.WriteLine(val2);
int abs1 = Math.Abs(val1);
int abs2 = Math.Abs(val2);
Console.WriteLine("After...");
Console.WriteLine(abs1);
Console.WriteLine(abs2);
}
}
Output
Before... 77 -88 After... 77 88
Advertisements