Rozwiązuję teraz proste zadanie z internetu: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 20.
O ile program dobrze wybiera mi z podanego zakresu liczby podzielne przez 3 lub 5, ale potem źle je sumuje. Nie wiem, co zrobiłam źle.
#include <iostream>
int main() {
int a = 3;
int b = 5;
int sumaA = 0;
int sumaB = 0;
for (a = 3; a <= 20; a++) {
if (a % 3 == 0)
std::cout << a << std::endl;
}
sumaA += a;
std::cout << "==============" << std::endl;
std::cout << sumaA << std::endl;
std::cout << "========================" << std::endl;
for (b = 5; b <= 20; b++) {
if (b % 5 == 0)
std::cout << b << std::endl;
}
sumaB += b;
std::cout << "==============" << std::endl;
std::cout << sumaB << std::endl;
std::cout << "========================" << std::endl;
std::cout << "Suma wszystkich liczb podzielnych przez 3 lub 5 wynosi: " << sumaA + sumaB << std::endl;
return 0;
}