In this example, i’ll show you How to get absolute value without **Math.Abs** function.

In mathematics, the absolute value or modulus of a real number x, denoted |x|, is the non-negative value of x without regard to its sign. Namely, |x| = x if x is positive, and |x| = −x if x is negative (in which case −x is positive), and |0| = 0. For example, the absolute value of 3 is 3, and the absolute value of −3 is also 3. The absolute value of a number may be thought of as its distance from zero.

**Solution1:**

**C# Code:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
static void Main(string[] args) { Console.WriteLine("Enter a number:"); int x = Convert.ToInt32(Console.ReadLine()); if (x < 0) { x *= -1; } Console.WriteLine(x); Console.ReadKey(); } |

**Solution 2: **

Return absolute value without using abs function.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace absolute_value { class Program { static void Main(string[] args) { Console.Write("Enter a number:"); int x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Absolute Value : "+result(x)); Console.ReadKey(); } public static int result(int n) { if (n < 0) { return -n; } return n; } } } |

**Output:**