Find negative numbers from array of integers using C# program

Learn, how to find out negative numbers from list of integers?
[Last updated : March 19, 2023]

Finding negative numbers from an array

Given array of integers, and we have to all negative numbers.

To find out negative numbers from array: we check each number; if number is less than zero then it will be a negative number. We traverse array of integer, if it is negative number then we will print that number of console.

Example

Input:
18, -13, 23, -12, 27

Output:
18 is not a negative number because it is not less than zero.
-13 is a negative number because it is less than zero.
23 is not a negative number because it is not less than zero.
-12 is a negative number because it is less than zero.
27 is not a negative number because it is not less than zero.

C# program to find negative numbers from an array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      int i = 0;
      int[] arr = new int[5];

      Console.WriteLine("Enter array elements : ");
      for (i = 0; i < arr.Length; i++) {
        Console.Write("Element[" + (i + 1) + "]: ");
        arr[i] = int.Parse(Console.ReadLine());
      }

      Console.WriteLine("List of negative numbers : ");
      for (i = 0; i < arr.Length; i++) {
        if (arr[i] < 0)
          Console.Write(arr[i] + " ");
      }
      Console.WriteLine();
    }
  }
}

Output

Enter array elements :
Element[1]: 12
Element[2]: -13
Element[3]: 14
Element[4]: -15
Element[5]: -17
List of negative numbers :
-13  -15  -17

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.