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

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;
}
主站蜘蛛池模板: 江西省| 福海县| 襄樊市| 常熟市| 兴国县| 宁波市| 贞丰县| 富宁县| 会理县| 从化市| 通化县| 巩义市| SHOW| 博罗县| 墨江| 内黄县| 皮山县| 泰兴市| 大悟县| 泽普县| 辉县市| 东兴市| 迁西县| 庐江县| 建始县| 伊吾县| 波密县| 井陉县| 洪洞县| 兰考县| 株洲市| 齐河县| 内乡县| 清镇市| 会理县| 防城港市| 富平县| 荆州市| 株洲市| 佳木斯市| 禄劝|