I need help for number (odd and even number in list)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fawwwaz
    New Member
    • Apr 2013
    • 2

    I need help for number (odd and even number in list)

    hi all

    I need help

    Example:
    I have 3 list

    list1 already contains number(Even and Odd)
    list2 empty
    list3 empty


    I want copy the even number in list2 and the odd number in list3 :) and I have one button.

    this method need a Loop

    I need your help :)

    notes: I use visual basic studio 2012
    Thanks
    Last edited by fawwwaz; Apr 4 '13, 11:15 PM. Reason: Add notes
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What do you have so far?

    Comment

    • vijay6
      New Member
      • Mar 2010
      • 158

      #3
      Hey fawwwaz, Check the remainder when every element in 'List1' is divide by 2. If the remainder is 0 then add that 'List1' element to 'List2' else add that 'List1' element to 'List3'. Like this,

      Code:
      void b1_Click(object sender, EventArgs e)
      {
      l2 = new List<int>();
      l3 = new List<int>();
      
      for (int i = 0; i < l1.Count; i++)
      {
      if (l1[i] % 2 == 0)
      l2.Add(l1[i]);
      else
      l3.Add(l1[i]);
      }
      
      Console.WriteLine("Iist 2");
                  
      foreach (var item in l2)
      {
      Console.WriteLine(item);
      }
      
      Console.WriteLine("Iist 3");
      
      foreach (var item in l3)
      {
      Console.WriteLine(item);
      }
      }

      Note: l1, l2 and l3 are List<int> type and b1 is Button.

      Comment

      • fawwwaz
        New Member
        • Apr 2013
        • 2

        #4
        Originally posted by vijay6
        Hey fawwwaz, Check the remainder when every element in 'List1' is divide by 2. If the remainder is 0 then add that 'List1' element to 'List2' else add that 'List1' element to 'List3'. Like this,

        Code:
        void b1_Click(object sender, EventArgs e)
        {
        l2 = new List<int>();
        l3 = new List<int>();
        
        for (int i = 0; i < l1.Count; i++)
        {
        if (l1[i] % 2 == 0)
        l2.Add(l1[i]);
        else
        l3.Add(l1[i]);
        }
        
        Console.WriteLine("Iist 2");
                    
        foreach (var item in l2)
        {
        Console.WriteLine(item);
        }
        
        Console.WriteLine("Iist 3");
        
        foreach (var item in l3)
        {
        Console.WriteLine(item);
        }
        }

        Note: l1, l2 and l3 are List<int> type and b1 is Button.

        thanks
        but it doesn't work

        I think the code is:

        Code:
          Dim reminder As Double
        
                For i = 0 To (listone.Items.Count() - 1)
                    If reminder Mod 2 = 0 Then
                        listtow.Items.Add(listone.Items.Item(i))
                    Else
                        listthree.Items.Add(listone.Items.Item(i))
                    End If
                Next
            End Sub
        but this code wrong its need something to solve
        Last edited by Rabbit; Apr 5 '13, 10:36 PM. Reason: Please use code tags when posting code.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Please use code tags when posting code.

          Use i instead of reminder.

          Comment

          • vijay6
            New Member
            • Mar 2010
            • 158

            #6
            Hey @fawwwaz you using 'List' or 'ListBox' in your code? If you using 'List' like as you mentioned in your question then my code is right. If you want Visual Basic code then use the following code,

            Code:
            Imports System
            Imports System.Collections.Generic
            Imports System.Windows.Forms
            
            Namespace WindowsFormsApplication1
            	Public Partial Class Form1
            		Inherits Form
            		Private listOne As List(Of Double)
            		Private listTwo As List(Of Double)
            		Private listThree As List(Of Double)
            		Private button1 As Button
            
            		Public Sub New()
            			InitializeComponent()
            		End Sub
            
            		Private Sub Form1_Load(sender As Object, e As EventArgs)
            			listOne = New List(Of Double)()
            			button1 = New Button()
            			button1.Text = "Split"
            			button1.Location = New System.Drawing.Point(100, 100)
            			button1.Click += button1_Click
            			Controls.Add(button1)
            
            			Dim i As Integer = 0
            			While i < 25
            				listOne.Add(Convert.ToDouble(i))
            				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
            			End While
            		End Sub
            
            
            		Private Sub button1_Click(sender As Object, e As EventArgs)
            			listTwo = New List(Of Double)()
            			listThree = New List(Of Double)()
            
            			Dim i As Integer = 0
            			While i < listOne.Count
            				If listOne(i) Mod 2 = 0 Then
            					listTwo.Add(listOne(i))
            				Else
            					listThree.Add(listOne(i))
            				End If
            				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
            			End While
            
            			Console.Write(vbLf & "Iist 1 ->")
            
            			Dim i As Integer = 0
            			While i < listOne.Count
            				Console.Write(" " + listOne(i))
            				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
            			End While
            
            			Console.Write(vbLf & "Iist 2 ->")
            
            			Dim i As Integer = 0
            			While i < listTwo.Count
            				Console.Write(" " + listTwo(i))
            				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
            			End While
            
            			Console.Write(vbLf & "Iist 3 ->")
            
            			Dim i As Integer = 0
            			While i < listThree.Count
            				Console.Write(" " + listThree(i))
            				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
            			End While
            		End Sub
            	End Class
            End Namespace

            Else if you want C# code then use the following code,

            Code:
            using System;
            using System.Collections.Generic;
            using System.Windows.Forms;
            
            namespace WindowsFormsApplication1
            {
                public partial class Form1 : Form
                {
            
                    List<double> listOne;
                    List<double> listTwo;
                    List<double> listThree;
                    Button button1;        
            
                    public Form1()
                    {
                        InitializeComponent();
                    }
            
                    private void Form1_Load(object sender, EventArgs e)
                    {
                        listOne = new List<double>();
                        button1 = new Button();
                        button1.Text = "Split";
                        button1.Location = new System.Drawing.Point(100, 100);
                        button1.Click += button1_Click;
                        Controls.Add(button1);
            
                        for (int i = 0; i < 25; i++)
                        {
                            listOne.Add(Convert.ToDouble(i));
                        }
                    }
            
            
                    void button1_Click(object sender, EventArgs e)
                    {
                        listTwo = new List<double>();
                        listThree = new List<double>();
            
                        for (int i = 0; i < listOne.Count; i++)
                        {
                            if (listOne[i] % 2 == 0)
                                listTwo.Add(listOne[i]);
                            else
                                listThree.Add(listOne[i]);
                        }
            
                        Console.Write("\nIist 1 ->");
            
                        for (int i = 0; i < listOne.Count; i++)
                        {
                            Console.Write(" " + listOne[i]);
                        }
            
                        Console.Write("\nIist 2 ->");
            
                        for (int i = 0; i < listTwo.Count; i++)
                        {
                            Console.Write(" " + listTwo[i]);
                        }
            
                        Console.Write("\nIist 3 ->");
            
                        for (int i = 0; i < listThree.Count; i++)
                        {
                            Console.Write(" " + listThree[i]);
                        }
                    }
                }
            }

            Comment

            Working...