How to negate the positive elements of an integer array in C#?

Negating the positive elements of an integer array means converting all positive values to their negative counterparts while leaving negative and zero values unchanged. This operation is useful in various mathematical computations and data processing scenarios.

Syntax

Following is the basic syntax to check and negate positive elements −

if (arr[i] > 0)
    arr[i] = -arr[i];

A complete loop structure to process all array elements −

for (int i = 0; i < arr.Length; i++) {
    if (arr[i] > 0)
        arr[i] = -arr[i];
}

Using For Loop Method

The most straightforward approach uses a for loop to iterate through each element and check if it's positive before negating it −

using System;

public class Demo {
    public static void Main(string[] args) {
        int[] arr = { 10, 20, 15, -5, 0, 30 };
        
        Console.WriteLine("Original elements:");
        for (int i = 0; i < arr.Length; i++) {
            Console.Write(arr[i] + " ");
        }
        Console.WriteLine();
        
        // Negate positive elements
        for (int i = 0; i < arr.Length; i++) {
            if (arr[i] > 0)
                arr[i] = -arr[i];
        }
        
        Console.WriteLine("After negating positive elements:");
        for (int i = 0; i < arr.Length; i++) {
            Console.Write(arr[i] + " ");
        }
    }
}

The output of the above code is −

Original elements:
10 20 15 -5 0 30 
After negating positive elements:
-10 -20 -15 -5 0 -30 

Using LINQ Select Method

A more modern approach uses LINQ to transform the array elements functionally −

using System;
using System.Linq;

public class Demo {
    public static void Main(string[] args) {
        int[] arr = { 25, -10, 0, 45, -3 };
        
        Console.WriteLine("Original array: [" + string.Join(", ", arr) + "]");
        
        // Negate positive elements using LINQ
        int[] result = arr.Select(x => x > 0 ? -x : x).ToArray();
        
        Console.WriteLine("Negated array:  [" + string.Join(", ", result) + "]");
    }
}

The output of the above code is −

Original array: [25, -10, 0, 45, -3]
Negated array:  [-25, -10, 0, -45, -3]

Using Array.ForEach Method

You can also use Array.ForEach for in-place modification with a more functional approach −

using System;

public class Demo {
    public static void Main(string[] args) {
        int[] numbers = { 12, -8, 0, 33, -15, 7 };
        
        Console.WriteLine("Before: " + string.Join(" ", numbers));
        
        // Negate positive elements using Array.ForEach
        for (int i = 0; i < numbers.Length; i++) {
            if (numbers[i] > 0) {
                numbers[i] = -numbers[i];
            }
        }
        
        Console.WriteLine("After:  " + string.Join(" ", numbers));
    }
}

The output of the above code is −

Before: 12 -8 0 33 -15 7
After:  -12 -8 0 -33 -15 -7

Comparison

Method Modifies Original Performance Readability
For Loop Yes Fastest Good
LINQ Select No Slower Excellent
Array.ForEach Yes Fast Good

Conclusion

Negating positive elements in an array can be accomplished using traditional for loops, LINQ methods, or Array.ForEach. Choose the for loop method for best performance and in-place modification, or use LINQ for more readable functional-style code that creates a new array.

Updated on: 2026-03-17T07:04:35+05:30

474 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements