Jump to content

C# Dizideki Çift Sayıları Listbox Yazdırma


lamont
 Share

Recommended Posts

Arkadaşlar diziye atanan sayının arasındaki çift sayılarını almak istedim sonuç 10 veriyor {1,2,3,4,5,6,7,8,9,10} bu şekilde yaptıgmda yalnış bir sonuç veriyor nerde yalnış yapıyorum for ile normalde çift sayıları toplayabiliyorum ama dizide bir yerde eksigim var nedir arkadaşlar
 
 private void button1_Click(object sender, EventArgs e)
        {

            int[] sayilar = {10 };
            int cifttop = 0;

           
            listBox1.Items.Clear();

            for (int i = 0; i < sayilar.Length ; i++)
            {
               
                    if (sayilar[i] % 2 == 0)
                    {
                        cifttop =cifttop + sayilar[i];

                        

                        listBox1.Items.Add(cifttop.ToString());


                    }
                }


            }
        }
    }

Edited by lamont
Link to comment
Share on other sites

  • Editor
            int[] sayilar = {10 };
            int cifttop = 0; 
           
            listBox1.Items.Clear();
 
            for (int i = 0; i < sayilar.Length ; i+=2)
            {              
               cifttop =cifttop + sayilar[i];                 
            }

            listBox1.Items.Add(cifttop.ToString());

Hala aynı mantık kurmuyorsun.

Link to comment
Share on other sites


arkadaşlar çözdüm kafam çok dalgınmış hatamı yeni anladım işte yaptıgım kod

 private void button1_Click(object sender, EventArgs e)
        {

            int[] sayilar = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int cifttop = 0;


            listBox1.Items.Clear();

            for (int i = 0; i < sayilar[0]; i++)
            {

               
                    if (sayilar[i] % 2 == 0)
                    {
                        cifttop = cifttop + sayilar[i];



                        listBox1.Items.Add(cifttop.ToString());


                    }
                }


            }
        }
    }
}
Edited by lamont
Link to comment
Share on other sites

  • Editor

Bu kod gayet doğru çalışıyor sonuç 30 işte.

             int[] sayilar = {1,2,3,4,5,6,7,8,9,10};
            int cifttop = 0;
 
           
            listBox1.Items.Clear();
 
            for (int i = 0; i < sayilar.Length ; i++)
            {
               
                    if (sayilar[i] % 2 == 0)
                    {
                        cifttop =cifttop + sayilar[i];
                    }
            }
            listBox1.Items.Add(cifttop.ToString());

listbox a neden ekliyon anlamadım ama ekleyeceksende for dan sonra yaz o kodu.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...