Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, so I\'m working on a sequence problem for discrete structures and I have two

ID: 3861733 • Letter: H

Question

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>

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 << " y is: " << y;
       z = n - 3;
       cout << " z = " << z;
       for (int i = 1; i <= z; i++)
       {
           while (i % 2 != 0)
           {
               sum += 1;
           }

       }
       total = y + sum;
       cout << " total is: "<< total << endl;;
   }

   if (n % 1 ==0) {
       y = (n - 1)*n;
       z = n - 2;
       for (int i = 1; i <= z; i++)
       {
           while (i % 2 != 0)
           {
               sum += 1;
           }
          
       }
       total = y + sum;
       cout << total << endl;
   }


}


Explanation / Answer

Below is the required code:-

Thank you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote