The source code to convert negative values of an array into positive is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
// C# program to convert negative values
//an integer array into positive.
using System;
class Demo
{
public static void Main()
{
int[] arr = { 10, -20, 30, -40, 50, -60, 70 };
int loop = 0;
for (loop = 0; loop < arr.Length; loop++)
{
if (arr[loop] < 0)
arr[loop] = -arr[loop];
}
Console.WriteLine("Array elements after conversion:");
for (loop = 0; loop < arr.Length; loop++)
{
Console.Write(arr[loop]+" ");
}
Console.WriteLine();
}
}
Output:
Array elements after conversion:
10 20 30 40 50 60 70
Press any key to continue . . .
Explanation:
In the above program, we created a class Demo that contains the Main() method. Here we created an array of integers that contains negative and positive integer elements.
for (loop = 0; loop < arr.Length; loop++)
{
if (arr[loop] < 0)
arr[loop] = -arr[loop];
}
In the above code, we converted negative integer values into positive values, and then we printed the elements of the array on the console screen.
Program:
The source code to convert negative values of an array into positive is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created a class Demo that contains the Main() method. Here we created an array of integers that contains negative and positive integer elements.
In the above code, we converted negative integer values into positive values, and then we printed the elements of the array on the console screen.