# Python Program to Find Value of y Mod (2 raised to power x)

In the previous article, we have discussed Python Program to Find Sum of Modulo K of First N Natural Numbers

Given two numbers x, y(integers) and the task is to find the value of given y modulus 2 raised to the power x.

Examples:

Example1:

Input:

Given x value = 5
Given y value = 9

Output:

The result of given y modulus 2 raised to the power x = 9

Example2:

Input:

Given x value = 3
Given y value = 12

Output:

The result of given y modulus 2 raised to the power x = 4

## Program to Find Value of y Mod (2 raised to power x) in python

Below are the ways to find the value of given y modulus 2 raised to the power x in python:

### Method #1: Using For Loop (Static Input)

Approach:

• Give the number x as static input and store it in a variable.
• Give the number y as static input and store it in another variable.
• Pass the given two numbers y and x as the arguments to the y_mod2powx function.
• Create a function to say y_mod2powx which takes the given two numbers y and x as the arguments and returns the value of given y modulus 2 raised to the power x.
• Calculate the value of 2 raised to the power x using the pow() function and store it in a variable.
• Calculate the value of the given y value modulus the above result and store it in another variable rslt.
• Return the above result value rslt.
• Print the value of given y modulus 2 raised to the power x.
• The Exit of the Program.

Below is the implementation:

# Create a function to say y_mod2powx which takes the given two numbers y and x as
# the arguments and returns the value of given y modulus 2 raised to the power x.

def y_mod2powx(gvn_yval, x):
# Calculate the value of 2 raised to the power x using the pow() function and store it
# in a variable.
p = pow(2, gvn_xval)
# Calculate the value of the given y value modulus the above result and store it in
# another variable rslt.
rslt = gvn_yval % p
# Return the above result value rslt.
return (rslt)

# Give the number x as static input and store it in a variable.
gvn_xval = 5
# Give the number y as static input and store it in another variable.
gvn_yval = 9
# Pass the given two numbers y and x as the arguments to the y_mod2powx function.
# Print the value of given y modulus 2 raised to the power x.
print("The result of given y modulus 2 raised to the power x =",
y_mod2powx(gvn_yval, gvn_xval))


Output:

The result of given y modulus 2 raised to the power x = 9

### Method #2: Using For loop (User Input)

Approach:

• Give the number x as user input using the int(input()) function and store it in a variable.
• Give the number y as user input using the int(input()) function and store it in another variable.
• Pass the given two numbers y and x as the arguments to the y_mod2powx function.
• Create a function to say y_mod2powx which takes the given two numbers y and x as the arguments and returns the value of given y modulus 2 raised to the power x.
• Calculate the value of 2 raised to the power x using the pow() function and store it in a variable.
• Calculate the value of the given y value modulus the above result and store it in another variable rslt.
• Return the above result value rslt.
• Print the value of given y modulus 2 raised to the power x.
• The Exit of the Program.

Below is the implementation:

# Create a function to say y_mod2powx which takes the given two numbers y and x as
# the arguments and returns the value of given y modulus 2 raised to the power x.

def y_mod2powx(gvn_yval, x):
# Calculate the value of 2 raised to the power x using the pow() function and store it
# in a variable.
p = pow(2, gvn_xval)
# Calculate the value of the given y value modulus the above result and store it in
# another variable rslt.
rslt = gvn_yval % p
# Return the above result value rslt.
return (rslt)

# Give the number x as user input using the int(input()) function and
# store it in a variable.
gvn_xval = int(input("Enter some random number = "))
# Give the number y as user input using the int(input()) function and
# store it in another variable.
gvn_yval = int(input("Enter some random number = "))
# Pass the given two numbers y and x as the arguments to the y_mod2powx function.
# Print the value of given y modulus 2 raised to the power x.
print("The result of given y modulus 2 raised to the power x =",
y_mod2powx(gvn_yval, gvn_xval))


Output:

Enter some random number = 3
Enter some random number = 12
The result of given y modulus 2 raised to the power x = 4

Grab the opportunity and utilize the Python Program Code Examples over here to prepare basic and advanced topics too with ease and clear all your doubts.