0

Help in java

Hello . Write a java program that uses the ternary operatot to determine if an integer number is odd or even Example: Num=10 Expected Output : Num is even With explain comment kindly I try but i cant

1st Nov 2020, 5:25 PM
STOP
STOP - avatar
23 Answers
+ 4
Ternary is not complicated. Is only a short form for if-else (condition) ? True statement : False statement; Example: String result = (x%y == z) ? "result true" : "result false" Here result becomes "result true" if condition x%y == z is true, or "result false" if condition is false.
1st Nov 2020, 5:47 PM
Coding Cat
Coding Cat - avatar
+ 3
Where's your try?
1st Nov 2020, 5:39 PM
Artem 🇺🇦
Artem 🇺🇦 - avatar
+ 2
Bushra Zhrani Tag name of to whom you are asking... My way : System.out.println("Num is "+(num%2==0 ? "even" :"odd"));
2nd Nov 2020, 3:29 PM
Jayakrishna 🇮🇳
+ 1
But your code works, what is the question?
1st Nov 2020, 5:50 PM
Coding Cat
Coding Cat - avatar
+ 1
Ardalan instead of this others thread, make your own new question.. You may also can get quick response... Your works fine hoping you closed in correctly, ( it is truncated) but here package creation and using of user defined packages don't work.. It not support,not access your local storage. So it may work fine in your system but not here.. Hope it helps...
3rd Nov 2020, 12:23 PM
Jayakrishna 🇮🇳
0
this is my try
1st Nov 2020, 5:25 PM
STOP
STOP - avatar
0
i posted why its delet
1st Nov 2020, 5:39 PM
STOP
STOP - avatar
0
wait
1st Nov 2020, 5:39 PM
STOP
STOP - avatar
0
Bushra Zhrani in your 1st code it should be num%2==0. You did right in 2nd code. What is problem, if those not passing test cases then use proper spacing and capital letters as required farmat mentioned in description. And may there need to take input instead of 10 ? What is problem explain it clearly..
1st Nov 2020, 8:44 PM
Jayakrishna 🇮🇳
0
i need another way to write code
2nd Nov 2020, 7:33 AM
STOP
STOP - avatar
0
Another way...? Not understood.. Can you clarify...
2nd Nov 2020, 12:36 PM
Jayakrishna 🇮🇳
0
Try this one public class Program { public static void main(String[] args) { int num=10; if(num%2 ==0){ System.out.println("num is even"); } else{ System .out.println("num is odd"); } } }
2nd Nov 2020, 1:24 PM
Sanjay Modi
Sanjay Modi - avatar
0
can you code it without if statment??
2nd Nov 2020, 3:21 PM
STOP
STOP - avatar
0
Jayakrishna🇮🇳 can you write the code by insert code ❤️
2nd Nov 2020, 3:33 PM
STOP
STOP - avatar
0
Yes.Bushra Zhrani But why don't you trying it yourself something.. First just complete linked topic in java. You can do it.. Ok. For taking input : //First create Scanner object like Scanner in = new Scanner(System.in); //Next take input by int num= in.nextInt(); Before import scanner class, you can find it here how to do that... Combine all and post total code if you need any further help... https://www.sololearn.com/learn/Java/2220/
2nd Nov 2020, 3:45 PM
Jayakrishna 🇮🇳
0
Bushra Zhrani What do you want? The solution from Jayakrishna🇮🇳 is similar to your code from yesterday. https://code.sololearn.com/cjtTFYAxhMfw/?ref=app
2nd Nov 2020, 3:45 PM
Coding Cat
Coding Cat - avatar
0
Coding CAT why in my code we use string and Jayakrishna🇮🇳 we don't
2nd Nov 2020, 4:03 PM
STOP
STOP - avatar
0
Ok, you are first saving the output in a String variable and then printing this variable. But you can do that in one step. Simple example: String out = "123"; print (out); Or shorter: print("123"); That's why w/o String variable. You got it?
2nd Nov 2020, 4:12 PM
Coding Cat
Coding Cat - avatar
0
Bushra Zhrani in your code, you are storing ternary instruction result into a string and then using it in printing.. But in my code, am not storing, directly using in printing.. If you need result further, store it else no need, waste of storage..
2nd Nov 2020, 4:13 PM
Jayakrishna 🇮🇳