# Conditionally assign a value without using conditional and arithmetic operators

Given 4 integers a, b, y, and x, where x can only either 0 and 1 only. The ask is as follows:

```If 'x' is 0,
Assign value 'a' to variable 'y'
Else (If 'x' is 1)
Assign value 'b' to variable 'y'.
```

Note: – You are not allowed to use any conditional operator (including ternary operator) or any arithmetic operator ( +, -, *, /).

Examples :

```Input :  a = 5 , b = 10, x = 1
Output :  y = 10

Input : a = 5, b = 10 , x = 0
Output :  y = 5
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

```An Idea is to simply store both 'a' and  'b'
in an array  at 0th and 1th index respectively.
Then store value to 'y' by taking 'x' as index.
```

Below is implementation

## C/C++

 `// C/C++ program  to assign value to y according ` `// to value of x ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to assign value to y according ` `// to value of x ` `int` `assignValue(``int` `a, ``int` `b, ``int` `x) ` `{ ` `    ``int` `y; ` `    ``int` `arr; ` ` `  `    ``// Store both values in an array ` `    ``// value 'a' at 0th index ` `    ``arr = a; ` ` `  `    ``// Value 'b' at 1th index ` `    ``arr = b; ` ` `  `    ``// Assign value to 'y' taking 'x' as index ` `    ``y = arr[x]; ` ` `  `    ``return` `y; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a = 5; ` `    ``int` `b = 10; ` `    ``int` `x = 0; ` ` `  `    ``cout << ``"Value assigned to 'y' is "` `         ``<< assignValue(a, b, x); ` `    ``return` `0; ` `} `

## Java

 `// Java program  to assign value to y according ` `// to value of x ` ` `  `public` `class` `GFG  ` `{ ` `    ``static` `int` `assignValue(``int` `a, ``int` `b, ``int` `x) ` `    ``{ ` `        ``int` `y; ` `        ``int` `arr[] = ``new` `int``[``2``]; ` `      `  `        ``// Store both values in an array ` `        ``// value 'a' at 0th index ` `        ``arr[``0``] = a; ` `      `  `        ``// Value 'b' at 1th index ` `        ``arr[``1``] = b; ` `      `  `        ``// Assign value to 'y' taking 'x' as index ` `        ``y = arr[x]; ` `      `  `        ``return` `y; ` `    ``} ` `     `  `    ``// Driver Method ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `         ``int` `a = ``5``; ` `         ``int` `b = ``10``; ` `         ``int` `x = ``0``; ` `          `  `         ``System.out.println(``"Value assigned to 'y' is "` `+ ` `                             ``assignValue(a, b, x)); ` `    ``} ` `} `

/div>

## Python 3

 `# Python 3 program to assign value to  ` `# y according to value of x ` ` `  `# Function to assign value to y  ` `# according to value of x ` `def` `assignValue(a, b, x): ` ` `  `    ``arr ``=` `[``0``] ``*` `2` ` `  `    ``# Store both values in an array ` `    ``# value 'a' at 0th index ` `    ``arr[``0``] ``=` `a ` ` `  `    ``# Value 'b' at 1th index ` `    ``arr[``1``] ``=` `b ` ` `  `    ``# Assign value to 'y' taking 'x'  ` `    ``# as index ` `    ``y ``=` `arr[x] ` ` `  `    ``return` `y ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``a ``=` `5` `    ``b ``=` `10` `    ``x ``=` `0` ` `  `    ``print``(``"Value assigned to 'y' is"``, ` `                ``assignValue(a, b, x)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to assign value to y according ` `// to value of x ` `using` `System; ` ` `  `public` `class` `GFG { ` `     `  `    ``static` `int` `assignValue(``int` `a, ``int` `b, ``int` `x) ` `    ``{ ` `        ``int` `y; ` `        ``int` `[]arr = ``new` `int``; ` `     `  `        ``// Store both values in an array ` `        ``// value 'a' at 0th index ` `        ``arr = a; ` `     `  `        ``// Value 'b' at 1th index ` `        ``arr = b; ` `     `  `        ``// Assign value to 'y' taking 'x'  ` `        ``// as index ` `        ``y = arr[x]; ` `     `  `        ``return` `y; ` `    ``} ` `     `  `    ``// Driver Method ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `a = 5; ` `        ``int` `b = 10; ` `        ``int` `x = 0; ` `         `  `        ``Console.Write(``"Value assigned to "` `        ``+ ``"'y' is "` `+ assignValue(a, b, x)); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal. `

## PHP

 ` `

Output :

```Value assigned to 'y' is 5
```

## tags:

C++ CBSE - Class 11 school-programming CPP