How to output 3 when you enter 5 and output 5 when input is 3 without if, switch in one row code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to output 3 when you enter 5 and output 5 when input is 3 without if, switch in one row code?

without if, switch ...

11th Mar 2017, 5:39 PM
Johon Хушбоков
Johon Хушбоков - avatar
4 Answers
+ 12
o_o Srsly? I guess there might be a way you can do it with bitwise operators or something, but I dunno how to do that!
11th Mar 2017, 6:20 PM
Tamra
Tamra - avatar
+ 11
Use the ternary operator, ?:. Nest them together like this: cout << (input == 5) ? "3" : ((input == 3) ? "5" : "something else")))
11th Mar 2017, 5:49 PM
Tamra
Tamra - avatar
+ 5
cin>>n; cout << 8-n; 😅😅
11th Mar 2017, 6:21 PM
Johon Хушбоков
Johon Хушбоков - avatar
+ 2
without ternary operator 😊
11th Mar 2017, 6:11 PM
Johon Хушбоков
Johon Хушбоков - avatar