0
Follow
0
View

How to solve the problem of # divisible #?

ddl223 注册会员
2023-02-27 20:14
, sum ); .
< !- - - - - >
dqj0618 注册会员
2023-02-27 20:14

Loop through all integers between 1 and 1000. For each integer, determine whether it is divisible by 4 and 9. If not, add it to sum. And then we just print sum.

int sum = 0;
for (int i = 1; i <= 1000; i++) {
    if (i % 4 != 0 && i % 9 != 0) {
        sum += i;
    }
}
cout << sum << endl;


wjj16387142 注册会员
2023-02-27 20:14

Refer to the following:

#include 
int fun(int  k)
{
    int m = 0;
    while (k >= 1)
    {
        if ((k % 4 != 0) && (k % 9 != 0))
        {
            m += k;
        }
        k--;
    }
    return m;
}
 
int main()
{
    printf("%d\n", fun(1000));
    return 0;
}

cszhangjinbo 注册会员
2023-02-27 20:14

Walk through 1-1000, i / 4! = 0 and i / 9 ! =0
sum += i

About the Author

Question Info

Publish Time
2023-02-27 20:14
Update Time
2023-02-27 20:14