Thursday, July 30, 2009

Please help me with this.. it's all about if-else condition and using conditional operator?

make a c program that will ask the user to enter a number. the program should display "POSITIVE ODD","NEGATIVE ODD", "POSITIVE EVEN:, and "NEGATIVE EVEN"


depending on the number entered.

Please help me with this.. it's all about if-else condition and using conditional operator?
Using conditional (ternary) operator:





#include %26lt;iostream.h%26gt;





void main(){


signed int num;





cout %26lt;%26lt; "Enter Number" %26lt;%26lt; endl;


cin %26gt;%26gt; num;


cout %26lt;%26lt; ((num %26gt; 0) ? "POSITIVE " : "NEGATIVE ");


cout %26lt;%26lt; ((num %26amp; 1) ? "ODD" : "EVEN");


}





Using if - else statements:





#include %26lt;iostream.h%26gt;





void main(){


signed int num;





cout %26lt;%26lt; "Enter Number" %26lt;%26lt; endl;


cin %26gt;%26gt; num;


if(num %26gt; 0)


{


cout %26lt;%26lt; "POSITIVE ";


}


else


{


cout %26lt;%26lt; "NEGATIVE ";


}


if(num %26amp; 1)


{


cout %26lt;%26lt; "ODD";


}


else


{


cout %26lt;%26lt; "EVEN";


}


}





Enter Number


32


POSITIVE EVEN





Enter Number


-33


NEGATIVE ODD
Reply:int x;


cout%26gt;%26gt;"Enter a number"%26gt;%26gt;endln;


cin%26lt;%26lt;x;





if (x%26gt;0)


{


if (x%2==0) cout%26gt;%26gt;"Positive even";


else cout%26gt;%26gt;"Positive odd";


}


else


{


if (x%2==0) cout%26gt;%26gt;"Negative even";


else cout%26gt;%26gt;"Negative even";


}


No comments:

Post a Comment