# Programming puzzle (Assign value without any control statement)

Given four integers ‘a’, ‘b’, ‘y’ and ‘x’, where ‘x’ can only be either zero or one. Your task is as follows:

• If ‘x’ is zero assign value ‘a’ to the variable ‘y’
• If ‘x’ is one assign value ‘b’ to the variable ‘y’.

It is not allowed to use any conditional operator (including the ternary operator).

Examples :

```Input  : a = 3, b = 7,  x = 1
Output : y = 7

Input : a = 3, b = 7, x = 0
Output : y = 3
```

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

The idea is to create an array of size two where the first element is ‘a’ and the second element is ‘b’. We use x as an index in the array.

## C++

 `// CPP program to pick a value among two ` `// according to value of a third variable. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns a if x is 0 and returns ` `// b if x is 1. ` `int` `assignValue(``int` `a, ``int` `b, ``bool` `x) ` `{ ` `    ``int` `arr[] = {a, b}; ` ` `  `    ``return``(arr[x]); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `y = assignValue(3, 7, 0); ` `    ``cout << y; ` `    ``return` `0; ` `} `

## Java

 `// Java program to pick a value among two ` `// according to value of a third variable. ` `class` `GFG { ` ` `  `    ``// Returns a if x is 0 and returns ` `    ``// b if x is 1. ` `    ``static` `int` `assignValue(``int` `a, ``int` `b, ``int` `x) ` `    ``{ ` `        ``int` `arr[] = {a, b}; ` ` `  `        ``return` `(arr[x]); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `y = assignValue(``3``, ``7``, ``0``); ` `        ``System.out.println(y); ` `    ``} ` `} ` ` `  `// This code is contributed by  Smitha Dinesh Semwal. `

## Python3

 `# Python 3 program to ` `# pick a value among two ` `# according to value ` `# of a third variable. ` ` `  `# Returns a if x ` `# is 0 and returns ` `# b if x is 1. ` `def` `assignValue(a, b, x): ` ` `  `    ``arr ``=` `[a, b] ` `    ``return``(arr[x]) ` ` `  ` `  `# Driver code ` `y ``=` `assignValue(``3``, ``7``, ``0``) ` ` `  `print``(y) ` ` `  `# This code is contributed by ` `# Smitha Dinesh Semwal `

## C#

 `// C# program to pick a value among two ` `// according to value of a third variable. ` `using` `System; ` ` `  `public` `class` `GFG { ` `  `  `    ``// Returns a if x is 0 and returns ` `    ``// b if x is 1. ` `    ``static` `int` `assignValue(``int` `a, ``int` `b, ``int` `x) ` `    ``{ ` `        ``int` `[]arr = {a, b}; ` `  `  `        ``return` `(arr[x]); ` `    ``} ` `  `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `y = assignValue(3, 7, 0); ` `        ``Console.WriteLine(y); ` `    ``} ` `} ` `// This code is contributed by PrinciRaj1992 `

## PHP

 ` `

Output :

``` 3
```

Alternate Solution :

## C++

 `// C++ program to pick a value among two ` `// according to value of a third variable. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns a if x is 0 and returns ` `// b if x is 1. ` `int` `assignValue(``int` `a, ``int` `b, ``bool` `x) ` `{    ` `    ``return` `(1 - x)*a + x*b; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `y = assignValue(3, 7, 0); ` `    ``cout << y; ` `    ``return` `0; ` `} `

## Java

 `// Java program to pick a value among two ` `// according to value of a third variable. ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `// Returns a if x is 0 and returns ` `// b if x is 1. ` `static` `int` `assignValue(``int` `a, ``int` `b, ``int` `x) ` `{  ` `    ``return` `(``1` `- x) * a + x * b; ` `} ` `         `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `y = assignValue(``3``, ``7``, ``0``); ` `         `  `    ``System.out.println(y); ` `} ` `} ` ` `  `// This code is contributed by ShubhamCoder `

## Python3

 `# Python3 program to pick a value among two ` `# according to the value of a third variable. ` ` `  `# Returns a if x is 0 and returns ` `# b if x is 1. ` `def` `assignValue(a, b, x): ` `     `  `    ``return` `(``1` `-` `x) ``*` `a ``+` `x ``*` `b ` `     `  `# Driver code ` `y ``=` `assignValue(``3``, ``7``, ``0``) ` `print``(y) ` ` `  `# This code is contributed by ShubhamCoder `

## C#

 `// C# program to pick a value among two ` `// according to value of a third variable. ` `using` `System; ` ` `  `class` `GFG { ` ` `  `// Returns a if x is 0 and returns ` `// b if x is 1. ` `static` `int` `assignValue(``int` `a, ``int` `b, ``int` `x) ` `{  ` `    ``return` `(1 - x) * a + x * b; ` `} ` `     `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `y = assignValue(3, 7, 0); ` `     `  `    ``Console.WriteLine(y); ` `} ` `} ` ` `  `// This code is contributed by ShubhamCoder `

Output :

```3
```

Time Complexity: O(1)
Thanks to Forrest Smith for suggesting the above solution.

This article is contributed by Maajid Bashir. 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.

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.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.