Home » .Net

Find positive numbers from array of integers using C# program

In this C# program, we will learn how to find out positive numbers from list of integers? Here we will create an array that contains list of integers.

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

To find out positive numbers from array: we check each number, if number is greater than or equal to zero then it will be a positive number. We traverse array of integer, if it is positive number then we will print that number of console.

For example we have list of integers:

18, -13, 23, -12, 27

18 is a positive number because it is greater than equal to zero.
-13 is not a positive number because it is not greater than equal to zero.
23 is a positive number because it is greater than equal to zero.
-12 is not a positive number because it is not greater than equal to zero.
27 is a positive number because it is greater than equal to zero.

Consider the example:

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 positive 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 positive numbers :
12 14




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.