1. 程式人生 > >C#實現1000以內的完數

C#實現1000以內的完數

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _04完數
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            //i用來判斷這個數
            for (int i = 2; i < 1000; i++)
            {
                //記錄每個數的因數之和
                int res = 0;
                //判斷i的所有的因數,並求其之和
                //string str = "1";
                //j用來記錄約數
                for (int j = 1; j < i; j++)
                {
                    int yu = i % j;
                    if (yu==0)
                    {
                        res = res + j;
                    }
                }
                if (res==i)
                {
                    label1.Text = label1.Text + i.ToString() + ",";
                }
            }
            
        }
    }
}