Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

need help finding odd/ even using modulus

i know this must be a simple problem, but i dont use modulus too much...and my book is no help...
i need to determine if the number is odd / even then do the correct func.
this is what i have...but it doesnt work right

[code]
//do - while x != 1

if ( x%2) //if even
x = x/2.0;

else
x = 3*x + 1;

cout << x << endl;
[/code]

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    when (x%2) == 0 the number is even, otherwise it is odd
    [code]
    #include
    using namespace std;

    int main()
    {
    for(int x = 0; x < 10; x++)
    {
    if( (x % 2) == 0)
    cout << "x: " << x << " is even." << endl;
    else
    cout << "x: " << x << " is odd." << endl;
    }
    cin.ignore();

    return 0;
    }
    [/code]

Sign In or Register to comment.