Given three integers a, b and c where c can be either 0 or 1. Without using any arithmetic, relational and conditional operators set the value of a variable x based on below rules –

If c = 0x= a Else // Note c is binaryx= b.

Examples:

Input:a = 5, b = 10, c = 0;Output:x = 5Input:a = 5, b = 10, c = 1;Output:x = 10

**Solution 1:** Using arithmetic operators

If we are allowed to use arithmetic operators, we can easily calculate x by using any one of below expressions –

x = ((1 - c) * a) + (c * b) OR x = (a + b) - (!c * b) - (c * a); OR x = (a * !c) | (b * c);

`#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `calculate(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `return` `((1 - c) * a) + (c * b); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `a = 5, b = 10, c = 0; ` ` ` ` ` `int` `x = calculate(a, b, c); ` ` ` `cout << x << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

5

**Solution 2: **Without using arithmetic operators

The idea is to construct an array of size 2 such that index 0 of the array stores value of variable ‘a’ and index 1 value of variable b. Now we return value at index 0 or at index 1 of the array based on value of variable c.

`#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `calculate(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `int` `arr[] = {a, b}; ` ` ` `return` `*(arr + c); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `a = 5, b = 10, c = 1; ` ` ` ` ` `int` `x = calculate(a, b, c); ` ` ` `cout << x << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

10

This article is contributed by **Aditya Goel **. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Conditionally assign a value without using conditional and arithmetic operators
- Implementing ternary operator without any conditional statement
- Print "Even" or "Odd" without using conditional statement
- Conditional or Ternary Operator (?:) in C/C++
- Difference between Relational operator(==) and std::string::compare() in C++
- Can we access global variable if there is a local variable with same name?
- Internal static variable vs. External static variable with Examples in C
- How to sum two integers without using arithmetic operators in C/C++?
- To find sum of two numbers without using any operator
- vector::operator= and vector::operator[ ] in C++ STL
- deque::operator= and deque::operator[] in C++ STL
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Operator Overloading '<<' and '>>' operator in a linked list class
- Comparing String objects using Relational Operators in C++
- Operators in C | Set 2 (Relational and Logical Operators)
- Relational Operators on STL Array in C++
- Using a variable as format specifier in C
- Sum of array using pointer arithmetic
- Ratio Manipulations in C++ | Set 1 (Arithmetic)
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)