123

Màu nền
Font chữ
Font size
Chiều cao dòng

Bài 1:  Viết chương trình tính tổng dãy số nguyên chia hết cho 3 từ m đến n với m và n được nhập từ hộp textbox có tên text1 và text2, kết quả hiển thị trên hộp textbox có tên là text3.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace C1

{

    public partial class Form1 : Form

    {

        public int m, n, S=0;

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            if(((int.TryParse(textBox1.Text,out m))&&(int.TryParse(textBox2.Text,out n))))

            {

            for (int i = m; i <= n; i++)

            {

                S += i;

            }

            textBox3.Text = S.ToString();

            }

        }

    }

}

Bài 2:  Viết chương trình tìm và hiển thị dãy các số nguyên lẻ từ m đến n với m và n được nhập từ hộp textbox có tên text1 và text2 (2đ), kết quả hiển thị trên hộp textbox có tên là text3 (2đ).

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace C1

{

    public partial class Form1 : Form

    {

        public int m, n, S=0;

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            if (((int.TryParse(textBox1.Text, out m)) && (int.TryParse(textBox2.Text, out n))))

            {

                for (int i = m; i <= n; i++)

                {

                    if (i % 2 != 0)

                    {

                        textBox3.Text = textBox3.Text + "  " + i.ToString();

                    }

                }

            }

        }

    }

}

Bài 3:Cho form gồm: 2 textbox: TextBox1, TextBox2

1 button: Button1

3 label: Label1, Label2, Label3

Lập trình để khi nhấn Button1 thì tổng, tổng chẵn, tổng lẻ của dãy số được nhập từ Textbox1, TextBox2 hiển thị lên lần lượt Label1, Label2, Label3.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace C1

{

    public partial class Form1 : Form

    {

        public int m, n, S = 0, Schan = 0, Sle = 0;

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            if (((int.TryParse(textBox1.Text, out m)) && (int.TryParse(textBox2.Text, out n))))

            {

                for (int i = m; i <= n; i++)

                {

                    S += i;

                    if (i % 2 == 0)

                    {

                        Schan += i;

                    }

                    else Sle += i;

                }

            }

            label1.Text = S.ToString();

            label2.Text = Schan.ToString();

            label3.Text = Sle.ToString();

        }

    }

}

Bạn đang đọc truyện trên: Truyen2U.Pro