Absolute Value - C# Programming Exercise

This C# exercise aims to develop a program that calculates (and displays) the absolute value of a number x. The absolute value of a number is its distance from zero on the number line, regardless of whether the number is positive or negative. If the number is positive, its absolute value is exactly the same number, but if the number is negative, its absolute value is the same number but with a positive sign. This exercise will be done in two different ways within the same program: first using an if conditional structure and then using the conditional operator (?). Both methods allow evaluating the condition of whether a number is negative and, if so, changing its sign. This type of exercise is ideal for learning how to use conditionals in C# and understanding the different approaches to performing the same calculation, thus improving control flow programming skills. It is also useful for applying the concept of ternary operators, which allow writing conditions more compactly and efficiently. Additionally, this exercise reinforces the use of control flow structures, which are essential in any type of programming.

 Category

Flow Control

 Exercise

Absolute Value

 Objective

Write a C# program 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's negative, its absolute value is -x.

Do it in two different ways in the same program: using "if" and using the "conditional operator" (?)

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        // Prompt the user to enter a number
        Console.Write("Enter a number: ");
        int x = int.Parse(Console.ReadLine()); // Read and convert the input to an integer

        // Using if statement to calculate absolute value
        int absoluteValueIf = x; // Initially assume the absolute value is x
        if (x < 0) // Check if the number is negative
        {
            absoluteValueIf = -x; // If negative, make it positive by negating it
        }

        // Display the result using the 'if' method
        Console.WriteLine($"Absolute value using 'if': {absoluteValueIf}");

        // Using the conditional operator (?) to calculate absolute value
        int absoluteValueConditional = (x < 0) ? -x : x; // If x is negative, make it positive using ? operator

        // Display the result using the conditional operator method
        Console.WriteLine($"Absolute value using conditional operator: {absoluteValueConditional}");
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Hollow rectangle

    This C# exercise aims to develop a program that prompts the user for a symbol, a width, and a height, then displays a hollow rectangle of the spe...

  •  Statistics

    This C# exercise aims to develop a program that allows the user to input several numbers and calculate basic statistical operations such as sum, average, mi...

  •  Switch

    This C# exercise aims to develop a program that, given a numerical grade, displays the corresponding text grade according to the following equivalence: 9 and ...

  •  Conditional operator, positive & smaller

    This C# exercise aims to develop a program that asks the user for two numbers and uses the conditional operator (?) to answer the following questions: ...

  •  Prime number

    This C# exercise aims to develop a program that asks the user for an integer and determines if it is a prime number or not. A prime number is one...

  •  Give change

    This exercise in C# aims to develop a program that calculates the change for a purchase, using the largest possible coins or bills. The program s...