【王道JAVA】【程序 9 求完数】

题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如 6=1+2+3.编程找出 1000 以内的所有完数。

public class WangDao {
	public static void main(String[] args){
		System.out.println("The perfact numbers between 1-1000 are: ");
		for (int i = 1; i <= 1000; i++) {
			int sum = 0;
			for (int j = 1; j <= i/2+1; j++ ) {
				if (i % j == 0) {
					sum += j;
				}
			}
			if (sum == i) {
				System.out.println(i);
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/YelloJesse/article/details/89375892