Take input from user and show number less than 10 in Console Application using C#

bakhtawar
 
on Oct 11, 2016 02:27 AM
3438 Views

I am trying that when i input 10 random numbers on console then message will be display these numbers are less than 10 

e..g  if i input these random numbers 10,9,12,45,120,2,3,1,9,14

then  message display  9,2,3,1,9 are less than 10

What I have tried:

public void p1()
{
    try
    {
        Console.WriteLine("Enter numbers");

        int num = Convert.ToInt32(Console.ReadLine());

        if (num < 10)
        {
            Console.WriteLine("less num", num);
            Console.ReadLine();

        }
        else
        {
            Console.WriteLine("Not less than num", num);
            Console.ReadLine();
        }
    }
    catch (Exception ex)
    {
        throw new Exception();
    }
}

but this work like that when i enter only 1 e.g. if i enter 9 number then message display less number and when i enter 12 then message display Not less than num where as i want  i input 10 numbers and from that program identifies which numbers are less than 10  how i do that CHECK THIS GIF

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 11, 2016 02:31 AM
on Oct 13, 2016 02:20 AM

Hi bakhtawar,

Refer the below code.

static void Main(string[] args)
{
    try
    {
        Console.WriteLine("Enter numbers: ");
        string[] nums = Console.ReadLine().Split(',');
        List<string> lessThan10 = new List<string>();
        for (int i = 0; i < nums.Length; i++)
        {
            if (Convert.ToInt16(nums[i]) < 10)
            {
                lessThan10.Add(nums[i]);
            }
        }

        if (lessThan10.Count > 0)
        {
            string[] finalList = lessThan10.ToArray();
            Console.WriteLine(string.Join(",", finalList) + " are less than 10");
        }
        else
        {
            Console.WriteLine("Non of the number/s are less than 10");
        }
        Console.ReadLine();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

OutPut

Enter numbers:

10,9,12,45,120,2,3,1,9,14

9,2,3,1,9 are less than 10