Tuesday, July 14, 2009

PLEASE HELP ME WITH THIS: using multiple if-else and ternary operator...?

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


please use multiple if-else condition...





please!!!!! i just need it for our midterm....





also using ternary operator...











example output:


enter a number: 56


POSITIVE EVEN!





please help me with this peeps...





hope u understand...


thanks!!

PLEASE HELP ME WITH THIS: using multiple if-else and ternary 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";


}


}








OUPUT:





Enter Number


32


POSITIVE EVEN





Enter Number


-33


NEGATIVE ODD
Reply:not know c, but if you can translate from basic...


cls


do


input "enter a number";num


a=int(num/2)


b=num-a


if a=b then a$="even" else a$="odd"


If num%26lt;0 then c$="negative"


if num%26gt;0 then c$="positive"


Print"number "num is "c$;" and";a$


loop until inkey$%26lt;%26gt;""
Reply:#include %26lt;iostream.h%26gt;





main() {


int num;


cout%26lt;%26lt;"Enter a number: "


cin%26gt;%26gt;num;


if(num %26gt;= 0) {


if(num%2 != 0) {


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


}


else {


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


}


}


else {


if(num%2 != 0) {


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


}


else {


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


}


}


}





Thats the basic structure, although you probably won't be able to copy/paste it and have it work. The guy above me doesn't use the mod operator (the % sign), so his code is slightly more complicated than mine. I think mine is what your teacher is looking for. You should really read a book or ask the teacher if you have further problems. One thing I've learned from going to Yahoo Answers for support on programming bugs is that they aren't as helpful as the teacher/professor.





Happy Programming.


No comments:

Post a Comment