Calculate and deduce the sum of all cubes in the interval from n to m ([n; m]), and the odd-squared product. Check: when n = 1, m = 6, must be output: sum of Cubes 288 Square Sql. 225
I'm not sure if the google translator translated it right but for the odd numbers it should be :
1 * 1 * 3 * 3 * 5 * 5 = 225
And with the Cube
2 * 2 * 2 + 4 * 4 * 4 + 6 * 6 * 6 = 288
I have to use For and If inside For. I'm really lost with this.
I know I should't ask for homework tasks but I'm like totally stuck. I started learning 5 months ago and we barely finished with if. Now we just started For and using If inside of it.
#include<iostream>
usingnamespace std;
int main() {
int n = 1, m = 6;
int result = 1;
for (int i = n; i < m + 1; i++) {
if (i % 2 == 0) {
continue;
}
else {
result *= (i * i);
}
}
cout << result << endl;
result = 0;
for (int i = n; i < m + 1; i++) {
if (i % 2 == 0) {
result += (i * i * i);
}
}
cout << result << endl;
return 0;
}
#include <iostream>
int main()
{
int n = 1;
int m = 6;
bool odd = n&1;
int prod = 1;
int sum = 0;
for (int i=n; i<=m; ++i)
{
odd ? prod *= i*i : sum += i*i*i;
odd = !odd;
}
std::cout << prod << std::endl << sum << std::endl;
}
#include<iostream>
usingnamespace std;
void stuffAndThings(int n, int m, int &result1, int &result2) {
n % 2 ? result1 *= (n*n) : result2 += (n*n*n);
n < m ? stuffAndThings(++n, m, result1, result2) : cout << result1 << endl << result2;
}
int main() {
int n = 1, m = 6;
int result1 = 1, result2 = 0;
stuffAndThings(n, m, result1, result2);
return 0;
}