- 1
Write a c++ program to check the given number is less than 100 or greater than 100 by using ternary operator
5 Réponses
+ 4
( if this question homework)? true : false
+ 2
This will return 0 if num is equal to 100, -1 if num is less than 100, and 1 if num is greater than 100. You can modify the type and values returned to get your desired result.
int x = num == 100 ? 0 : (num < 100 ? -1 : 1 );
+ 2
@Martin Taylor I agree. I'm usually verbose with my code to begin with at least and then will refactor it to reduce code size and for optimization etc. I typically write my code in a way that seldom needs comments and will leave it verbose if the refactoring obfuscates the code to a point in which I feel I need a comment to understand it.
Given the OP's question I thought this solution would be a decent answer using the ternary operator.