The Wayback Machine - https://web.archive.org/web/20240620134613/https://www.geeksforgeeks.org/conditionally-assign-value-without-using-conditional-arithmetic-operators/
Open In App

Conditionally assign a value without using conditional and arithmetic operators

Last Updated : 13 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given 4 integers a, b, y, and x, where x can assume the values of either 0 or 1 only. The following question is asked:

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 the 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

Asked in: Google Interview 

Solution 1:

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

Below is the implementation of the above approach:

C++




// C/C++ program to assign value to y according
// to value of x
  
#include <iostream>
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[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
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));
    }
}


Python3




# 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[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
    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




<?php 
// PHP program to assign value 
// to y according to value of x 
  
// Function to assign value 
// to y according to value of x 
function assignValue($a, $b, $x) 
{ 
    $y; 
    $arr = array(0, 0); 
  
    // 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 
$a = 5; 
$b = 10; 
$x = 0; 
  
echo "Value assigned to 'y' is " . 
        assignValue($a, $b, $x); 
  
// This code is contributed by Anuj_67 
?> 


Javascript




<script>
// javascript program to assign value to y according
// to value of x
  
  
    function assignValue(a , b , x) {
        var y;
        var arr = Array(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
      
        var a = 5;
        var b = 10;
        var x = 0;
  
        document.write("Value assigned to 'y' is " 
        + assignValue(a, b, x));
  
// This code is contributed by todaysgaurav 
</script>


Output

Value assigned to 'y' is 5

Solution 2: 

Using bitwise AND operator.

C++




// C/C++ program to assign value to y according
// to value of x
  
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    int a = 5;
    int b = 10;
    int x = 1;
    int y;
  
    if (x & 1)
        y = b;
    else
        y = a;
    cout << "Value assigned to 'y' is " << y << endl;
    return 0;
}


Java




// Java program to assign value to y according
// to value of x
import java.io.*;
class GFG
{
  
  // Driver Code
  public static void main (String[] args) 
  {    
    int a = 5;
    int b = 10;
    int x = 1;
    int y;
    if ((x & 1) != 0)
      y = b;
    else
      y = a;
    System.out.println("Value assigned to 'y' is " + y);
  }
}
  
// This code is contributed by avanitrachhadiya2155


Python3




# Python3 program to assign value to y 
# according to value of x
  
# Driver Code
a = 5
b = 10
x = 1
y = 0
  
if ((x & 1) != 0):
    y = b
else:
    y = a
  
print("Value assigned to 'y' is ", y)
  
# This code is contributed by ab2127


C#




// C# program to assign value to y according
// to value of x
using System;
public class GFG
{
  
  // Driver Code
  static public void Main ()
  {
  
    int a = 5;
    int b = 10;
    int x = 1;
    int y;
    if ((x & 1) != 0)
      y = b;
    else
      y = a;
    Console.WriteLine("Value assigned to 'y' is " + y);
  }
}
  
// This code is contributed by rag2127


Javascript




<script>
  
// Javascript program to assign value to y according
// to value of x
  
    // Driver Code
      
        var a = 5;
        var b = 10;
        var x = 1;
        var y;
        if ((x & 1) != 0)
            y = b;
        else
            y = a;
        document.write("Value assigned to 'y' is " + y);
  
// This code contributed by Rajput-Ji 
  
</script>


Output

Value assigned to 'y' is 10

Reference: https://www.careercup.com/question?id=5135296679116800

 

 



Similar Reads

How to sum two integers without using arithmetic operators in C/C++?
Given two integers a and b, how can we evaluate the sum a + b without using operators such as +, -, ++, --, ...? Method 1 (Using pointers)An interesting way would be: C/C++ Code // May not work with C++ compilers and // may produce warnings in C. // Returns sum of 'a' and 'b' int sum(int a, int b) { char *p = a; return (int)&amp;amp;p[b]; } Despite
4 min read
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
6 min read
Print "Even" or "Odd" without using conditional statement
Write a program that accepts a number from the user and prints "Even" if the entered number is even and prints "Odd" if the number is odd. You are not allowed to use any comparison (==, &lt;,&gt;,...etc) or conditional statements (if, else, switch, ternary operator,. Etc). Method 1 Below is a tricky code can be used to print "Even" or "Odd" accordi
4 min read
Implementing ternary operator without any conditional statement
How to implement ternary operator in C++ without using conditional statements.In the following condition: a ? b: c If a is true, b will be executed. Otherwise, c will be executed.We can assume a, b and c as values. 1. Using Binary Operator We can code the equation as : Result = (!!a)*b + (!a)*c In above equation, if a is true, the result will be b.
4 min read
C++ Arithmetic Operators
Arithmetic Operators in C++ are used to perform arithmetic or mathematical operations on the operands. For example, ‘+’ is used for addition, ‘-‘ is used for subtraction, ‘*’ is used for multiplication, etc. In simple terms, arithmetic operators are used to perform arithmetic operations on variables and data; they follow the same relationship betwe
3 min read
C++ Program To Find Power Without Using Multiplication(*) And Division(/) Operators
Method 1 (Using Nested Loops):We can calculate power by using repeated addition. For example to calculate 5^6. 1) First 5 times add 5, we get 25. (5^2) 2) Then 5 times add 25, we get 125. (5^3) 3) Then 5 times add 125, we get 625 (5^4) 4) Then 5 times add 625, we get 3125 (5^5) 5) Then 5 times add 3125, we get 15625 (5^6)  C/C++ Code // C++ code fo
3 min read
Program to Assign grades to a student using Nested If Else
Given an integer array marks, which comprises of marks scored by a student (out of 100) in different subjects, the task is to assign a grade to the student. The grade is found out by taking the percentage of the marks scored by the student. The percentage is calculated as: The grade is assigned using the following rules: PercentageGrade90 and above
7 min read
Difference Between deque::cbegin and deque::assign in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::at in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::back in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Deque::assign deque::assign is
3 min read
Practice Tags :