Adding equations

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include <iostream>

using namespace 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;
	}


}
Topic archived. No new replies allowed.