How to display Absolute value of a number in C#?

CsharpProgrammingServer Side Programming

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

 Live Demo

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
raja
Published on 09-Aug-2018 14:51:46
Advertisements