#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int odd(int nValue)
{

nValue= nValue -1;
return nValue;

}


int main()
{

int nValue;

    cout << "Enter a Positive Number: ";
    cin >> nValue;

            while ((nValue>2) && (nValue % 2 == 0))
            {
                    nValue = nValue -2;
                    cout << nValue <<endl;
            }


            while (nValue>2)
                {
                nValue = odd(nValue);

                 if(nValue % 2 == 0)
                        {
                    //nValue = nValue -2;
                    cout << nValue <<endl;
                        }

                }



    system("pause");
    return 0;
}

Why does the above program work?
when in the second while loop i have

//nValue = nValue -2;

Where when its not it counts down by 4. I cant see the way the flow structure in my program works.

Write a program that prompts the user for a positive number and uses a repetition control structure to print every even number less than the number and greater than zero (0) in reverse order. For example, if the user enters 19, the numbers 18, 16, 14, 12, 10, 8, 6, 4 and 2 are printed.

Ok i think i understand it now trying to say it is a different story.

upon failing the first loop it enters the second.

a. subtracts 1
b. compares to see if divided by 2 and gives a remainder of 0
c. if does; it prints the number.

Then it starts again this time failing to print because number is odd again.

works but its ugly. suggestions on how to improve?

Edited 5 Years Ago by alonewolf23: n/a

I think the problem is that you're checking if the numbers are even every time even though this is in no way necessary. If your first number is even, you can't subtract 2 to get an odd number.

See here:

#include <iostream>

using namespace std;
      
     
int main()
{
 int nValue;
 cout << "Enter a Number: ";
 cin >> nValue;
 if (nValue % 2 != 0) //Checks for an odd number
    nValue -=1;
 
 while (nValue >= 2)
 {
  cout << nValue <<" ";
  nValue -= 2;
 }

cout << endl;

return 0;
}

This is what i ended up with after realizing what it was doing finally.
I find your code much more compact and simple and I never thought to just check if it was odd first then go into my loop which as you have shown is the better choice. I was so focused on I knew what to do with an Even input that I left checking for an odd number til last which made it backwards in my program. I will try to remember to look at what I can take away to make two problems the same and use one loop for next time rather than two.

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int odd(int nValue)
{

nValue= nValue -1;
return nValue;

}


int main()
{

int nValue;

    cout << "Enter a Positive Number: ";
    cin >> nValue;

            while ((nValue>2) && (nValue % 2 == 0))
            {
                    nValue = nValue -2;
                    cout << nValue <<endl;
            }

            nValue = odd(nValue);

            while (nValue>2)
            {
                    nValue = nValue -2;
                    cout << nValue <<endl;
            }



    system("pause");
    return 0;
}

Also, remember that if you want your program to print the "2" as well, it needs to check for greater than or equal to 2. You would achieve this with the operator ">=". This is because the "while loop" checks for the condition before execution, and since 2 is not greater than 2, it will not print it.

This question has already been answered. Start a new discussion instead.