 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to negate the positive elements of an integer array in C#?
The following is the array and its elements −
int[] arr = { 10, 20, 15 }; Set negative value to positive elements.
if (arr[i] > 0) arr[i] = -arr[i];
Loop the above until the length of the array.
for (int i = 0; i < arr.Length; i++) {    Console.WriteLine(arr[i]);    if (arr[i] > 0)    arr[i] = -arr[i]; } Let us see the complete example.
Example
using System; public class Demo {    public static void Main(string[] args) {       int[] arr = { 10, 20, 15 };       Console.WriteLine("Displaying elements...");       for (int i = 0; i < arr.Length; i++) {          Console.WriteLine(arr[i]);          if (arr[i] > 0)          arr[i] = -arr[i];       }       Console.WriteLine("Displaying negated elements...");       for (int i = 0; i < arr.Length; i++) {          Console.WriteLine(arr[i]);         }    } }  Output
Displaying elements... 10 20 15 Displaying negated elements... -10 -20 -15
Advertisements
 