Q:

C# program to convert negative values an integer array into positive

belongs to collection: C# Basic Programs | array programs

0

C# program to convert negative values an integer array into positive

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

// 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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to get the length of a jagged array usi... >>
<< C# program to implement indexer for an integer arr...