Good Afternoon,

I want to convert this if-else statement to a switch statement.
The if-statement is:

int x;
cout<<"GIve me an int and I tell you whether is even of odd ";
cin>>x;
if (x%2==0)
cout<<x<<" is even."<<endl;
else
cout<<x<<" is odd."<<endl;

Recommended Answers

All 4 Replies

Where I 'm having problems is dealing with x%2==0.
So far I have this:

int x;
     cout<<"Give me an int and I tell you whether is even of odd ";
     cin>>x;

     switch (x)
     {       
        case 'A':
            x = %2;
            cout<<x<<" is even."<<endl;
        break;
        default:
             cout<<x<<" is odd."<<endl;
     }

try this

switch(x%2)
{
   case 0:  // even
   default:  // odd
 }

Ancient Dragon,

I try your advise, but the program execute the default case with every number

It was because of the character,
thank you very much for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.