C语言 找出 1000 以内的所有完数

一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6的因子为1,2,3,而6=1+2+3,因此6是"完数",编程序找出1000以内的所有完数,并按下面格式输出其因子:6 its factors are 1,2,3

这个程序找出 1000 以内的所有完数,并输出每个完数及其因子。

(如果因子和等于该数,则该数为完数。)

cpp 复制代码
#include <stdio.h>

int main() {
    printf("1000 以内的所有完数及其因子:\n");

    for (int i = 1; i < 1000; i++) {
        int sum = 0;
        // 找出所有因子
        for (int j = 1; j <= i / 2; j++) {
            if (i % j == 0) {
                sum += j;
            }
        }
        // 判断是否为完数
        if (sum == i) {
            printf("%d its factors are", i);
            for (int j = 1; j <= i / 2; j++) {
                if (i % j == 0) {
                    printf(" %d", j);
                }
            }
            printf("\n");
        }
    }

    return 0;
}

代码说明

  1. 遍历 1 到 999 的每个数。
  2. 对每个数,找到所有因子并累加。
  3. 如果因子和等于该数,则输出该完数及其因子。
相关推荐
m0_531237172 分钟前
C语言-编程实例2
c语言·开发语言
小白菜又菜6 分钟前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
python·算法·leetcode
不想看见4047 分钟前
01 Matrix 基本动态规划:二维--力扣101算法题解笔记
c++·算法·leetcode
多恩Stone10 分钟前
【3D-AICG 系列-12】Trellis 2 的 Shape VAE 的设计细节 Sparse Residual Autoencoding Layer
人工智能·python·算法·3d·aigc
踢足球092923 分钟前
寒假打卡:2026-2-23
数据结构·算法
zjxtxdy36 分钟前
C语言(续)
c语言·开发语言
田里的水稻1 小时前
FA_建图和定位(ML)-超宽带(UWB)定位
人工智能·算法·数学建模·机器人·自动驾驶
Navigator_Z1 小时前
LeetCode //C - 964. Least Operators to Express Number
c语言·算法·leetcode
郝学胜-神的一滴1 小时前
Effective Modern C++ 条款40:深入理解 Atomic 与 Volatile 的多线程语义
开发语言·c++·学习·算法·设计模式·架构
摸鱼仙人~1 小时前
算法题避坑指南:数组/循环范围的 `+1` 到底什么时候加?
算法