If else case and choose problem

Hello!
Today i got a question that

1. To check whether no. is even or odd
2. To check whether no. is positive or negative
depend upon users choice

Actually i am confused in if else case.. that how can i use if else In if else, hope you are getting me. so i have tried many program but i failed. here is one example that what i had done.

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
#include<iostream.h>
int main()
{
int y,z;

cout<<"\n Choose any option";
cout<<"\n 1. To check whether no. is even/odd";
cout<<"\n 2. To check whether no. is positive/negative \n";
cin>>y;

cout<<"\n Enter your no.";
cin>>z;

if(y==1 & z%2==0)
cout<<"\n No. is even";

else
cout<<"\n No. is odd"<<endl;

if(y==2 & z<0)
cout<<"\n no. is negative"<<endl;

else;
cout<<"\n no. is positive"<<endl;

return 0;
}


Please help....
Last edited on
You shall use logical && (and) operator instead of bitwise operator & in statements as the following for example

if(y==1 & z%2==0) // shall be &&
I can tell you that line 23 has a semicolon at the end when it shouldn't. Also, you are using a single & when you should be using two && (which means "and").

Now, the way I would think of implementing this is to have one set of an if/else if to check which option the user picked.

Then, inside of each of those blocks have some more if statements to check for what the user wanted.
Actually i have tried that too... when i use "&&" i got combined result
even/odd and positive negative both.
can anyne of you give me any example that how can i run it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch ( y )
{
   case 1:
      if ( z % 2 == 0 ) cout << "\nNo. is even" << endl;
      else cout << "\nNo. is odd" << endl;
      break;

   case 2:
      if ( 0 < z ) cout << "\nNo. is positive" << endl;
      else cout << "\nNo. is non-positive" << endl;

   default:
      cout << "\nYou should select from menu either 1 or 2" << endl;
      break;
}

Last edited on
Oh..!!! It's working thanks a lot....!! really thanks ... :)
Topic archived. No new replies allowed.