{"id":20563,"date":"2021-09-15T19:49:08","date_gmt":"2021-09-15T14:19:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=20563"},"modified":"2021-11-22T18:36:19","modified_gmt":"2021-11-22T13:06:19","slug":"python-program-for-swapping-three-variables-without-using-any-temporary-variable","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-swapping-three-variables-without-using-any-temporary-variable\/","title":{"rendered":"Python Program for Swapping Three Variables Without Using any Temporary Variable."},"content":{"rendered":"
In the previous article, we have discussed Python Program to Toggle the Last m Bits<\/a><\/p>\n Given three numbers and the task is to swap the given three numbers without using any temporary variable in python<\/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 swap given three numbers without using any temporary variable 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 Note: <\/strong>It’s worth noting that the above method causes the stack to overflow when the variables have huge values. When dealing with enormous numbers, the solution is to adopt a different strategy.<\/p>\n Access the big list of Python Programming Code Examples<\/a> with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.<\/p>\n In the previous article, we have discussed Python Program to Toggle the Last m Bits Given three numbers and the task is to swap the given three numbers without using any temporary variable in python Examples: Example1: Input: Given first number= 30 Given second number= 45 Given third number= 21 Output: The above given three …<\/p>\nGiven first number= 30\r\nGiven second number= 45\r\nGiven third number= 21<\/pre>\n
The above given three numbers before swapping :\r\nfirst number = 30 second number = 45 third number = 21\r\nThe above given three numbers after swapping without using temporary variable:\r\nfirst number = 21 second number = 30 third number = 45<\/pre>\n
Given first number= 60\r\nGiven second number= 80\r\nGiven third number= 70<\/pre>\n
The above given three numbers before swapping :\r\nfirst number = 60 second number = 80 third number = 70\r\nThe above given three numbers after swapping without using temporary variable:\r\nfirst number = 70 second number = 60 third number = 80<\/pre>\n
Program for Swapping Three Variables Without Using any Temporary Variable in Python<\/h2>\n
\n
Method #1: Using Arithmetic Operators (Static Input)<\/h3>\n
\n
# Give the first number as static input and store it in a variable.\r\nfst_numb = 30\r\n# Give the second number as static input and store it in another variable.\r\nscnd_numb = 45\r\n# Give the third number as static input and store it in another variable.\r\nthrd_numb = 21\r\nprint(\"The above given three numbers before swapping :\")\r\nprint(\"first number = \", fst_numb, \"second number = \",\r\n scnd_numb, \"third number = \", thrd_numb)\r\n# Add first, second, and third numbers and assign the result to the first number.\r\nfst_numb = fst_numb + scnd_numb + thrd_numb\r\n# Add second, and third numbers and subtract the result from the given first number.\r\n# Assign the result to the second number.\r\nscnd_numb = fst_numb - (scnd_numb+thrd_numb)\r\n# Add second, and third numbers and subtract the result from the given first number.\r\n# Assign the result to the given third number.\r\nthrd_numb = fst_numb - (scnd_numb+thrd_numb)\r\n# Add second, and third numbers and subtract the result from the given first number.\r\n# Assign the result to the given first number.\r\nfst_numb = fst_numb - (scnd_numb+thrd_numb)\r\n# Print the given three numbers after swapping without using a temporary variable.\r\nprint(\"The above given three numbers after swapping without using temporary variable:\")\r\nprint(\"first number = \", fst_numb, \"second number = \",\r\n scnd_numb, \"third number = \", thrd_numb)\r\n<\/pre>\n
The above given three numbers before swapping :\r\nfirst number = 30 second number = 45 third number = 21\r\nThe above given three numbers after swapping without using temporary variable:\r\nfirst number = 21 second number = 30 third number = 45<\/pre>\n
Method #2: Using Arithmetic Operators (User Input)<\/h3>\n
\n
# Give the first number as user input using the int(input()) function and\r\n# store it in a variable.\r\nfst_numb = int(input('Enter some random number = '))\r\n# Give the second number as user input using the int(input()) function and\r\n# store it in another variable.\r\nscnd_numb = int(input('Enter some random number = '))\r\n# Give the third number as user input using the int(input()) function and \r\n# store it in another variable.\r\nthrd_numb = int(input('Enter some random number = '))\r\nprint(\"The above given three numbers before swapping :\")\r\nprint(\"first number = \", fst_numb, \"second number = \",\r\n scnd_numb, \"third number = \", thrd_numb)\r\n# Add first, second, and third numbers and assign the result to the first number.\r\nfst_numb = fst_numb + scnd_numb + thrd_numb\r\n# Add second, and third numbers and subtract the result from the given first number.\r\n# Assign the result to the second number.\r\nscnd_numb = fst_numb - (scnd_numb+thrd_numb)\r\n# Add second, and third numbers and subtract the result from the given first number.\r\n# Assign the result to the given third number.\r\nthrd_numb = fst_numb - (scnd_numb+thrd_numb)\r\n# Add second, and third numbers and subtract the result from the given first number.\r\n# Assign the result to the given first number.\r\nfst_numb = fst_numb - (scnd_numb+thrd_numb)\r\n# Print the given three numbers after swapping without using a temporary variable.\r\nprint(\"The above given three numbers after swapping without using temporary variable:\")\r\nprint(\"first number = \", fst_numb, \"second number = \",\r\n scnd_numb, \"third number = \", thrd_numb)\r\n<\/pre>\n
Enter some random number = 60\r\nEnter some random number = 80\r\nEnter some random number = 70\r\nThe above given three numbers before swapping :\r\nfirst number = 60 second number = 80 third number = 70\r\nThe above given three numbers after swapping without using temporary variable:\r\nfirst number = 70 second number = 60 third number = 80<\/pre>\n
\n