C# program to demonstrate the use of Abs() method of Math class

Here, we are going to demonstrate the use of Abs() method of Math class in C#.Net.
Submitted by Nidhi, on April 26, 2021

Here, we will learn about the Abs() method of Math class. This method is used to return the absolute value of a given value. It means if we pass a positive value then it returns a positive value and if pass a negative value then it will also return a positive value.

Syntax:

There are the following overloaded method used to get absolute value,

Math.Abs(Decimal)
Math.Abs(Double)
Math.Abs(Int16)
Math.Abs(Int32)
Math.Abs(Int64)
Math.Abs(SByte)
Math.Abs(Single)

All the above-overloaded methods return value according to the type passed as a parameter.

We will understand all overloaded method with the help of program.

Program:

The source code to demonstrate the use of Abs() method of Math class is given below. The given program is compiled and executed successfully.

using System;
using System.IO;

class Sample
{
    //Entry point of Program
    static public void Main()
    {
        double[] doubles = { Double.MaxValue,Double.MinValue, 157.37, 120.00, -5.96 };
        decimal[] decimals = { Decimal.MaxValue,Decimal.MinValue, 157.37M, 120.00M, -5.96M };

        int[] integers = { Int32.MaxValue,Int32.MinValue, 157, 120, -5 };

        Console.WriteLine("Double Values:");
        foreach (double VAL in doubles)
            Console.WriteLine("\tMath.Abs({0}) : {1}", VAL, Math.Abs(VAL));

        Console.WriteLine("\nDecimal Values:");
        foreach (decimal VAL in decimals)
            Console.WriteLine("\tMath.Abs({0}) : {1}", VAL, Math.Abs(VAL));

        Console.WriteLine("\nInteger Values:");
        foreach (decimal VAL in integers)
            Console.WriteLine("\tMath.Abs({0}) : {1}", VAL, Math.Abs(VAL));   
    }
}

Output:

Double Values:
        Math.Abs(1.79769313486232E+308) : 1.79769313486232E+308
        Math.Abs(-1.79769313486232E+308) : 1.79769313486232E+308
        Math.Abs(157.37) : 157.37
        Math.Abs(120) : 120
        Math.Abs(-5.96) : 5.96

Decimal Values:
        Math.Abs(79228162514264337593543950335) : 79228162514264337593543950335
        Math.Abs(-79228162514264337593543950335) : 79228162514264337593543950335

        Math.Abs(157.37) : 157.37
        Math.Abs(120.00) : 120.00
        Math.Abs(-5.96) : 5.96

Integer Values:
        Math.Abs(2147483647) : 2147483647
        Math.Abs(-2147483648) : 2147483648
        Math.Abs(157) : 157
        Math.Abs(120) : 120
        Math.Abs(-5) : 5
Press any key to continue . . .

C# Math Class Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.