官术网_书友最值得收藏!

1. Sum of naturals pisible by 3 and 5

The solution to this problem is to iterate through all numbers from 3 (1 and 2 are not pisible by 3 so it does not make sense to test them) up to the limit entered by the user. Use the modulo operation to check that the rest of the pision of a number by 3 and 5 is 0. However, the trick to being able to sum up to a larger limit is to use long long and not int or long for the sum, which would result in an overflow before summing up to 100,000:

int main()
{
unsigned int limit = 0;
std::cout << "Upper limit:";
std::cin >> limit;

unsigned long long sum = 0;
for (unsigned int i = 3; i < limit; ++i)
{
if (i % 3 == 0 || i % 5 == 0)
sum += i;
}

std::cout << "sum=" << sum << std::endl;
}
主站蜘蛛池模板: 梧州市| 柞水县| 星座| 新干县| 铅山县| 六安市| 繁昌县| 安远县| 曲周县| 石渠县| 五台县| 突泉县| 呈贡县| 宁陵县| 铁岭县| 贵港市| 宜丰县| 长汀县| 卓尼县| 林口县| 罗江县| 家居| 洛宁县| 柞水县| 二连浩特市| 广州市| 杨浦区| 道孚县| 尉犁县| 延庆县| 西乌珠穆沁旗| 青海省| 江阴市| 涡阳县| 阜阳市| 潜山县| 岳西县| 鄱阳县| 黄山市| 安远县| 石棉县|