{"id":20473,"date":"2021-09-13T14:51:22","date_gmt":"2021-09-13T09:21:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=20473"},"modified":"2021-11-22T18:36:20","modified_gmt":"2021-11-22T13:06:20","slug":"python-program-for-efficient-way-to-multiply-with-7","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-efficient-way-to-multiply-with-7\/","title":{"rendered":"Python Program for Efficient Way to Multiply With 7"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators<\/a><\/p>\n 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 \u2013 n) by left shifting the number by 3 bits (you’ll receive 8n).<\/p>\n Give a number the task is to get the multiplication of the given number by 7 in an efficient way.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to get the multiplication of the given number by 7 in an efficient way in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more Example Python Programs<\/a> with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.<\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 \u2013 n) by left shifting the number by …<\/p>\nGiven Number = 5<\/pre>\n
After multiplying the given number{ 5 } by 7 = 35<\/pre>\n
Given Number = 3<\/pre>\n
After multiplying the given number{ 3 } by 7 = 21<\/pre>\n
Program for Efficient Way to Multiply With 7 in Python<\/h2>\n
\n
Method #1: Using Left Shift (<<) Operator (Static Input)<\/h3>\n
\n
# Create a recursive function to say effcnt_mult_bysevn() which takes the given number\r\n# as an argument and\u00a0returns the result of the given number multiplied by 7 in an\r\n# efficient way.\r\n\r\n\r\ndef effcnt_mult_bysevn(gvn_numb):\r\n # Inside the effcnt_mult_bysevn () function.\r\n # Calculate and the value of (gvn_numb << 3) - gvn_numb which gives the multiplication\r\n # of the given number by 7 and store it in a variable say rslt_mult.\r\n\r\n rslt_mult = (gvn_numb << 3) - gvn_numb\r\n # Return the value of rslt_mult(Which is the multiplication of the given number by 7).\r\n return (rslt_mult)\r\n\r\n\r\n# Inside the main code.\r\n# Give the number as static input and store it in a variable.\r\ngvn_numb = 5\r\n# Pass the given number as the argument to effcnt_mult_bysevn()\u00a0function and\r\n# store the result in a variable (fnl_rslt).\r\nfnl_rslt = effcnt_mult_bysevn(gvn_numb)\r\n# Print the fnl_rslt value.\r\nprint(\"After multiplying the given number{\", gvn_numb, \"} by 7 = \", fnl_rslt)\r\n<\/pre>\n
After multiplying the given number{ 5 } by 7 = 35<\/pre>\n
Method #2: Using Left Shift (<<) Operator (User Input)<\/h3>\n
\n
# Create a recursive function to say effcnt_mult_bysevn() which takes the given number\r\n# as an argument and\u00a0returns the result of the given number multiplied by 7 in an\r\n# efficient way.\r\n\r\n\r\ndef effcnt_mult_bysevn(gvn_numb):\r\n # Inside the effcnt_mult_bysevn () function.\r\n # Calculate and the value of (gvn_numb << 3) - gvn_numb which gives the multiplication\r\n # of the given number by 7 and store it in a variable say rslt_mult.\r\n\r\n rslt_mult = (gvn_numb << 3) - gvn_numb\r\n # Return the value of rslt_mult(Which is the multiplication of the given number by 7).\r\n return (rslt_mult)\r\n\r\n\r\n# Inside the main code.\r\n# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numb = int(input('Enter some random number = '))\r\n# Pass the given number as the argument to effcnt_mult_bysevn()\u00a0function and\r\n# store the result in a variable (fnl_rslt).\r\nfnl_rslt = effcnt_mult_bysevn(gvn_numb)\r\n# Print the fnl_rslt value.\r\nprint(\"After multiplying the given number{\", gvn_numb, \"} by 7 = \", fnl_rslt)\r\n<\/pre>\n
Enter some random number = 3\r\nAfter multiplying the given number{ 3 } by 7 = 21<\/pre>\n
\n