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
23 ответов
+ 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.
+ 3
Where's your try?
+ 2
Bushra Zhrani Tag name of to whom you are asking...
My way :
System.out.println("Num is "+(num%2==0 ? "even" :"odd"));
+ 1
But your code works, what is the question?
+ 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...
0
this is my try
0
i posted why its delet
0
wait
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..
0
i need another way to write code
0
Another way...? Not understood.. Can you clarify...
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");
}
}
}
0
can you code it without if statment??
0
Jayakrishna🇮🇳 can you write the code by insert code ❤️
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/
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
0
Coding CAT why in my code we use string and Jayakrishna🇮🇳 we don't
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?
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..