我为以下程序编写了代码,但我不明白如何使用函数。该问题要求我使用返回除数总和的函数。请看看这个问题和我的代码,并试图帮助我。查找使用循环的因子
问题:
编写发现并打印除数的总和为101和110 x的除数之间的所有 数C程序是那些数字 X除以没有余(例如10的因数为1, 2,5和10,它们的总和= 1 + 2 + 5 + 10 = 18,11 的因数为1和11,它们的和= 1 + 11 = 12,以及等等)。 你的程序还应该打印具有最大除数总和的数字(101至110)。
你的程序应该至少使用一个叫做div_sum的函数, 需要一个数字并返回除数的和。
我的代码:
#include <iostream>
#include <stdio.h>
int main()
{
int i=1, x=101, sum, smax=0, xmax=0;
for (x=101; x<=110; x++)
{ sum=0;
for(i=1; i<=x; i++)
{
if(x%i==0)
sum+=i;
}
if(sum>smax)
{
smax=sum;
xmax=x;
}
printf("The sum of factors of %d = %d\n",x,sum);
}
printf("The number that has the maximum sum of divisors is %d with the sum of %d",xmax,smax);
return 0;
}
的代码看起来不错,除了你不使用函数'div_sum'。 – Nabla