Найти сумму S и произведение P: а) четных чисел от 1 до n б) нечетных чисел от 1 до n в) чисел, кратных 3, от 1 до n.нужно написать программу в VisualStudio
int main() { int n; int sum_even = 0; int product_even = 1; int sum_odd = 0; int product_odd = 1; int sum_multiple_of_3 = 0; int product_multiple_of_3 = 1;
std::cout << "Enter a positive integer n: "; std::cin >> n; for (int i = 2; i <= n; i += 2) { sum_even += i; product_even *= i; } for (int i = 1; i <= n; i += 2) { sum_odd += i; product_odd *= i; } for (int i = 3; i <= n; i += 3) { sum_multiple_of_3 += i; product_multiple_of_3 *= i; } std::cout << "Sum of even numbers from 1 to " << n << ": " << sum_even << std::endl; std::cout << "Product of even numbers from 1 to " << n << ": " << product_even << std::endl; std::cout << "Sum of odd numbers from 1 to " << n << ": " << sum_odd << std::endl; std::cout << "Product of odd numbers from 1 to " << n << ": " << product_odd << std::endl; std::cout << "Sum of numbers multiple of 3 from 1 to " << n << ": " << sum_multiple_of_3 << std::endl; std::cout << "Product of numbers multiple of 3 from 1 to " << n << ": " << product_multiple_of_3 << std::endl; return 0;
int main() {
std::cout << "Enter a positive integer n: ";int n;
int sum_even = 0;
int product_even = 1;
int sum_odd = 0;
int product_odd = 1;
int sum_multiple_of_3 = 0;
int product_multiple_of_3 = 1;
std::cin >> n;
for (int i = 2; i <= n; i += 2) {
sum_even += i;
product_even *= i;
}
for (int i = 1; i <= n; i += 2) {
sum_odd += i;
product_odd *= i;
}
for (int i = 3; i <= n; i += 3) {
sum_multiple_of_3 += i;
product_multiple_of_3 *= i;
}
std::cout << "Sum of even numbers from 1 to " << n << ": " << sum_even << std::endl;
std::cout << "Product of even numbers from 1 to " << n << ": " << product_even << std::endl;
std::cout << "Sum of odd numbers from 1 to " << n << ": " << sum_odd << std::endl;
std::cout << "Product of odd numbers from 1 to " << n << ": " << product_odd << std::endl;
std::cout << "Sum of numbers multiple of 3 from 1 to " << n << ": " << sum_multiple_of_3 << std::endl;
std::cout << "Product of numbers multiple of 3 from 1 to " << n << ": " << product_multiple_of_3 << std::endl;
return 0;
}