Hi, so I'm working on a sequence problem for discrete structures and I have two formulas that I can use. So if user enters a positive odd n, the program has to calculate this formula: (1 + 3+...+(n-2))(n-1)(n)
And for even n: 2(n+1)[1+3+...+(n-3)](n-2)+2
the ... means sum of odds up to either n-2 or n-3.
This is the code I have so far but I'm not sure why it's not calculating it correctly:
#include <iostream>
usingnamespace std;
int main()
{
int n, y, z, one;
int sum = 0;;
int total;
cout << "Enter n: ";
cin >> n;
if (n % 2 == 0) {
y =((2*n + 1)*(n - 2) + 2);
cout << "\ny is: " << y;
z = n - 3;
cout << "\nz = " << z;
for (int i = 1; i <= z; i++)
{
if (i % 2 != 0)
{
sum += 1;
}
}
total = y + sum;
cout << "\ntotal is: "<< total << endl;;
}
else {
y = (n - 1)*n;
z = n - 2;
for (int i = 1; i <= z; i++)
{
if (i % 2 != 0)
{
sum += 1;
}
}
total = y + sum;
cout << total << endl;
}
}