ABS - Absolute value
Last updated:
8/23/2020
⁃
Difficulty:
Easy
Write a program in C# to calculate and display the absolute value of a number x.
If the number is positive, its absolute value is exactly the number x.
If it is negative, its absolute value is -x.
Input
7
Output
Absolute value is 7
Solution
using System;
public class AbsoluteValue
{
public static void Main(string[] args)
{
int x = Convert.ToInt32(Console.ReadLine());
int abs;
if (x < 0)
{
abs = -x;
}
else
{
abs = x;
}
Console.WriteLine("Absolute value is {0}", abs);
}
}