Enter 5 Elements : 10 7 8 45 67 Elements : -10 -7 -8 -45 -67
This is a C# Program to negate the positive elements of array.
This C# Program Negates the Positive Elements of Array.
The array elements are obtained from the user and the elements of the array are negated.
Here is source code of the C# Program to Negate the Positive Elements of Array. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Negate the Positive Elements of Array */ using System; using System.Collections.Generic; using System.Text; namespace program { class negate { public static void Main() { int[] a = new int[10]; Console.WriteLine("Enter 5 Elements : "); for (int i = 0; i < 5; i++) { a[i] = Convert.ToInt16(Console.ReadLine()); if (a[i] > 0) a[i] = -a[i]; } Console.WriteLine("Elements:"); for (int i = 0; i < 5; i++) { Console.WriteLine(a[i]); } Console.ReadLine(); } } }
This C# program, we are reading the value of 5 elements value using for loop and assign the value to ‘a[]’ array variable. If condition statement is used to check the value of ‘a[]’ variable is greater than 0. If the condition is true, then execute the statement. Assign the value of ‘a[]’ variable to the minus symbol of a[] variable. Hence using foreach loop print negate of positive elements of an array.
Enter 5 Elements : 10 7 8 45 67 Elements : -10 -7 -8 -45 -67