By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,866 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,866 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Apr 4 '13 #1

✓ answered by vijay6

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,

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.Windows.Forms
  4.  
  5. Namespace WindowsFormsApplication1
  6.     Public Partial Class Form1
  7.         Inherits Form
  8.         Private listOne As List(Of Double)
  9.         Private listTwo As List(Of Double)
  10.         Private listThree As List(Of Double)
  11.         Private button1 As Button
  12.  
  13.         Public Sub New()
  14.             InitializeComponent()
  15.         End Sub
  16.  
  17.         Private Sub Form1_Load(sender As Object, e As EventArgs)
  18.             listOne = New List(Of Double)()
  19.             button1 = New Button()
  20.             button1.Text = "Split"
  21.             button1.Location = New System.Drawing.Point(100, 100)
  22.             button1.Click += button1_Click
  23.             Controls.Add(button1)
  24.  
  25.             Dim i As Integer = 0
  26.             While i < 25
  27.                 listOne.Add(Convert.ToDouble(i))
  28.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  29.             End While
  30.         End Sub
  31.  
  32.  
  33.         Private Sub button1_Click(sender As Object, e As EventArgs)
  34.             listTwo = New List(Of Double)()
  35.             listThree = New List(Of Double)()
  36.  
  37.             Dim i As Integer = 0
  38.             While i < listOne.Count
  39.                 If listOne(i) Mod 2 = 0 Then
  40.                     listTwo.Add(listOne(i))
  41.                 Else
  42.                     listThree.Add(listOne(i))
  43.                 End If
  44.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  45.             End While
  46.  
  47.             Console.Write(vbLf & "Iist 1 ->")
  48.  
  49.             Dim i As Integer = 0
  50.             While i < listOne.Count
  51.                 Console.Write(" " + listOne(i))
  52.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  53.             End While
  54.  
  55.             Console.Write(vbLf & "Iist 2 ->")
  56.  
  57.             Dim i As Integer = 0
  58.             While i < listTwo.Count
  59.                 Console.Write(" " + listTwo(i))
  60.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  61.             End While
  62.  
  63.             Console.Write(vbLf & "Iist 3 ->")
  64.  
  65.             Dim i As Integer = 0
  66.             While i < listThree.Count
  67.                 Console.Write(" " + listThree(i))
  68.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  69.             End While
  70.         End Sub
  71.     End Class
  72. End Namespace

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

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4.  
  5. namespace WindowsFormsApplication1
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.  
  10.         List<double> listOne;
  11.         List<double> listTwo;
  12.         List<double> listThree;
  13.         Button button1;        
  14.  
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             listOne = new List<double>();
  23.             button1 = new Button();
  24.             button1.Text = "Split";
  25.             button1.Location = new System.Drawing.Point(100, 100);
  26.             button1.Click += button1_Click;
  27.             Controls.Add(button1);
  28.  
  29.             for (int i = 0; i < 25; i++)
  30.             {
  31.                 listOne.Add(Convert.ToDouble(i));
  32.             }
  33.         }
  34.  
  35.  
  36.         void button1_Click(object sender, EventArgs e)
  37.         {
  38.             listTwo = new List<double>();
  39.             listThree = new List<double>();
  40.  
  41.             for (int i = 0; i < listOne.Count; i++)
  42.             {
  43.                 if (listOne[i] % 2 == 0)
  44.                     listTwo.Add(listOne[i]);
  45.                 else
  46.                     listThree.Add(listOne[i]);
  47.             }
  48.  
  49.             Console.Write("\nIist 1 ->");
  50.  
  51.             for (int i = 0; i < listOne.Count; i++)
  52.             {
  53.                 Console.Write(" " + listOne[i]);
  54.             }
  55.  
  56.             Console.Write("\nIist 2 ->");
  57.  
  58.             for (int i = 0; i < listTwo.Count; i++)
  59.             {
  60.                 Console.Write(" " + listTwo[i]);
  61.             }
  62.  
  63.             Console.Write("\nIist 3 ->");
  64.  
  65.             for (int i = 0; i < listThree.Count; i++)
  66.             {
  67.                 Console.Write(" " + listThree[i]);
  68.             }
  69.         }
  70.     }
  71. }

Share this Question
Share on Google+
5 Replies


Rabbit
Expert Mod 10K+
P: 12,370
What do you have so far?
Apr 5 '13 #2

100+
P: 158
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,

Expand|Select|Wrap|Line Numbers
  1. void b1_Click(object sender, EventArgs e)
  2. {
  3. l2 = new List<int>();
  4. l3 = new List<int>();
  5.  
  6. for (int i = 0; i < l1.Count; i++)
  7. {
  8. if (l1[i] % 2 == 0)
  9. l2.Add(l1[i]);
  10. else
  11. l3.Add(l1[i]);
  12. }
  13.  
  14. Console.WriteLine("Iist 2");
  15.  
  16. foreach (var item in l2)
  17. {
  18. Console.WriteLine(item);
  19. }
  20.  
  21. Console.WriteLine("Iist 3");
  22.  
  23. foreach (var item in l3)
  24. {
  25. Console.WriteLine(item);
  26. }
  27. }

Note: l1, l2 and l3 are List<int> type and b1 is Button.
Apr 5 '13 #3

P: 2
@vijay6

thanks
but it doesn't work

I think the code is:

Expand|Select|Wrap|Line Numbers
  1.   Dim reminder As Double
  2.  
  3.         For i = 0 To (listone.Items.Count() - 1)
  4.             If reminder Mod 2 = 0 Then
  5.                 listtow.Items.Add(listone.Items.Item(i))
  6.             Else
  7.                 listthree.Items.Add(listone.Items.Item(i))
  8.             End If
  9.         Next
  10.     End Sub
but this code wrong its need something to solve
Apr 5 '13 #4

Rabbit
Expert Mod 10K+
P: 12,370
Please use code tags when posting code.

Use i instead of reminder.
Apr 5 '13 #5

100+
P: 158
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,

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.Windows.Forms
  4.  
  5. Namespace WindowsFormsApplication1
  6.     Public Partial Class Form1
  7.         Inherits Form
  8.         Private listOne As List(Of Double)
  9.         Private listTwo As List(Of Double)
  10.         Private listThree As List(Of Double)
  11.         Private button1 As Button
  12.  
  13.         Public Sub New()
  14.             InitializeComponent()
  15.         End Sub
  16.  
  17.         Private Sub Form1_Load(sender As Object, e As EventArgs)
  18.             listOne = New List(Of Double)()
  19.             button1 = New Button()
  20.             button1.Text = "Split"
  21.             button1.Location = New System.Drawing.Point(100, 100)
  22.             button1.Click += button1_Click
  23.             Controls.Add(button1)
  24.  
  25.             Dim i As Integer = 0
  26.             While i < 25
  27.                 listOne.Add(Convert.ToDouble(i))
  28.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  29.             End While
  30.         End Sub
  31.  
  32.  
  33.         Private Sub button1_Click(sender As Object, e As EventArgs)
  34.             listTwo = New List(Of Double)()
  35.             listThree = New List(Of Double)()
  36.  
  37.             Dim i As Integer = 0
  38.             While i < listOne.Count
  39.                 If listOne(i) Mod 2 = 0 Then
  40.                     listTwo.Add(listOne(i))
  41.                 Else
  42.                     listThree.Add(listOne(i))
  43.                 End If
  44.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  45.             End While
  46.  
  47.             Console.Write(vbLf & "Iist 1 ->")
  48.  
  49.             Dim i As Integer = 0
  50.             While i < listOne.Count
  51.                 Console.Write(" " + listOne(i))
  52.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  53.             End While
  54.  
  55.             Console.Write(vbLf & "Iist 2 ->")
  56.  
  57.             Dim i As Integer = 0
  58.             While i < listTwo.Count
  59.                 Console.Write(" " + listTwo(i))
  60.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  61.             End While
  62.  
  63.             Console.Write(vbLf & "Iist 3 ->")
  64.  
  65.             Dim i As Integer = 0
  66.             While i < listThree.Count
  67.                 Console.Write(" " + listThree(i))
  68.                 System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
  69.             End While
  70.         End Sub
  71.     End Class
  72. End Namespace

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

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4.  
  5. namespace WindowsFormsApplication1
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.  
  10.         List<double> listOne;
  11.         List<double> listTwo;
  12.         List<double> listThree;
  13.         Button button1;        
  14.  
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             listOne = new List<double>();
  23.             button1 = new Button();
  24.             button1.Text = "Split";
  25.             button1.Location = new System.Drawing.Point(100, 100);
  26.             button1.Click += button1_Click;
  27.             Controls.Add(button1);
  28.  
  29.             for (int i = 0; i < 25; i++)
  30.             {
  31.                 listOne.Add(Convert.ToDouble(i));
  32.             }
  33.         }
  34.  
  35.  
  36.         void button1_Click(object sender, EventArgs e)
  37.         {
  38.             listTwo = new List<double>();
  39.             listThree = new List<double>();
  40.  
  41.             for (int i = 0; i < listOne.Count; i++)
  42.             {
  43.                 if (listOne[i] % 2 == 0)
  44.                     listTwo.Add(listOne[i]);
  45.                 else
  46.                     listThree.Add(listOne[i]);
  47.             }
  48.  
  49.             Console.Write("\nIist 1 ->");
  50.  
  51.             for (int i = 0; i < listOne.Count; i++)
  52.             {
  53.                 Console.Write(" " + listOne[i]);
  54.             }
  55.  
  56.             Console.Write("\nIist 2 ->");
  57.  
  58.             for (int i = 0; i < listTwo.Count; i++)
  59.             {
  60.                 Console.Write(" " + listTwo[i]);
  61.             }
  62.  
  63.             Console.Write("\nIist 3 ->");
  64.  
  65.             for (int i = 0; i < listThree.Count; i++)
  66.             {
  67.                 Console.Write(" " + listThree[i]);
  68.             }
  69.         }
  70.     }
  71. }
Apr 8 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.