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.
#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;
}
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?
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;
}