当前位置:首页 C++ > 正文

c++编写一个函数,接受一个正整数 n,计算并返回其所有正因子的个数。

作者:野牛程序员:2023-08-11 20:27:44 C++阅读 2680

c++编写一个函数,接受一个正整数 n,计算并返回其所有正因子的个数。

#include <iostream>

int countPositiveFactors(int n) {
    int count = 0;
    for (int i = 1; i <= n; ++i) {
        if (n % i == 0) {
            count++;
        }
    }
    return count;
}

int main() {
    int num = 28;
    int count = countPositiveFactors(num);
    std::cout << "Number of positive factors of " << num << ": " << count << std::endl;
    return 0;
}


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击