C# Program to Convert a Positive Number into Negative

This is a C# Program to negate the positive elements of array.

Problem Description

This C# Program Negates the Positive Elements of Array.

Problem Solution

The array elements are obtained from the user and the elements of the array are negated.

Program/Source Code

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();
        }
    }
}
Program Explanation

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.

advertisement
advertisement
Runtime Test Cases
 
Enter 5 Elements :
10
7
8
45
67
Elements :
-10
-7
-8
-45
-67

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.