Python Program for Efficient Way to Multiply With 7

In the previous article, we have discussed Python Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators

The bitwise operator can be used to multiply a value by seven. Subtract the original number from the shifted number and return the difference (8n – n) by left shifting the number by 3 bits (you’ll receive 8n).

Give a number the task is to get the multiplication of the given number by 7 in an efficient way.

Examples:

Example1:

Input:

Given Number = 5

Output:

After multiplying the given number{ 5 } by 7 =  35

Example2:

Input:

Given Number = 3

Output:

After multiplying the given number{ 3 } by 7 =  21

Program for Efficient Way to Multiply With 7 in Python

Below are the ways to get the multiplication of the given number by 7 in an efficient way in Python:

Method #1: Using Left Shift (<<) Operator (Static Input)

Approach:

  • Create a recursive function to say effcnt_mult_bysevn() which takes the given number as an argument and returns the result of the given number multiplied by 7 in an efficient way.
  • Inside the effcnt_mult_bysevn () function.
  • Calculate and the value of (gvn_numb << 3) – gvn_numb which gives the multiplication of the given number by 7 and store it in a variable say rslt_mult.
  • Return the value of rslt_mult(Which is the multiplication of the given number by 7).
  • Inside the main code.
  • Give the number as static input and store it in a variable.
  • Pass the given number as the argument to effcnt_mult_bysevn() function and store the result in a variable (fnl_rslt).
  • Print the fnl_rslt value.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say effcnt_mult_bysevn() which takes the given number
# as an argument and returns the result of the given number multiplied by 7 in an
# efficient way.


def effcnt_mult_bysevn(gvn_numb):
  # Inside the effcnt_mult_bysevn () function.
  # Calculate and the value of (gvn_numb << 3) - gvn_numb which gives the multiplication
  # of the given number by 7 and store it in a variable say rslt_mult.

    rslt_mult = (gvn_numb << 3) - gvn_numb
  # Return the value of rslt_mult(Which is the multiplication of the given number by 7).
    return (rslt_mult)


# Inside the main code.
# Give the number as static input and store it in a variable.
gvn_numb = 5
# Pass the given number as the argument to effcnt_mult_bysevn() function and
# store the result in a variable (fnl_rslt).
fnl_rslt = effcnt_mult_bysevn(gvn_numb)
# Print the fnl_rslt value.
print("After multiplying the given number{", gvn_numb, "} by 7 = ", fnl_rslt)

Output:

After multiplying the given number{ 5 } by 7 =  35

Method #2: Using Left Shift (<<) Operator (User Input)

Approach:

  • Create a recursive function to say effcnt_mult_bysevn() which takes the given number as an argument and returns the result of the given number multiplied by 7 in an efficient way.
  • Inside the effcnt_mult_bysevn () function.
  • Calculate and the value of (gvn_numb << 3) – gvn_numb which gives the multiplication of the given number by 7 and store it in a variable say rslt_mult.
  • Return the value of rslt_mult(Which is the multiplication of the given number by 7).
  • Inside the main code.
  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as the argument to effcnt_mult_bysevn() function and store the result in a variable (fnl_rslt).
  • Print the fnl_rslt value.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say effcnt_mult_bysevn() which takes the given number
# as an argument and returns the result of the given number multiplied by 7 in an
# efficient way.


def effcnt_mult_bysevn(gvn_numb):
  # Inside the effcnt_mult_bysevn () function.
  # Calculate and the value of (gvn_numb << 3) - gvn_numb which gives the multiplication
  # of the given number by 7 and store it in a variable say rslt_mult.

    rslt_mult = (gvn_numb << 3) - gvn_numb
  # Return the value of rslt_mult(Which is the multiplication of the given number by 7).
    return (rslt_mult)


# Inside the main code.
# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input('Enter some random number = '))
# Pass the given number as the argument to effcnt_mult_bysevn() function and
# store the result in a variable (fnl_rslt).
fnl_rslt = effcnt_mult_bysevn(gvn_numb)
# Print the fnl_rslt value.
print("After multiplying the given number{", gvn_numb, "} by 7 = ", fnl_rslt)

Output:

Enter some random number = 3
After multiplying the given number{ 3 } by 7 = 21

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.