+ 2

Swapping of two numbers . Without temperory variable and with temperory variable. Python Program??

13th Mar 2018, 6:43 AM
Ambesh
6 Respuestas
+ 5
#1 a, b = b, a #2 c = a a = b b = c # del c
13th Mar 2018, 6:51 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
#include <stdio.h> int main() { double firstNumber, secondNumber; printf("Enter first number: \n"); scanf("%lf", &firstNumber); printf("Enter second number: "); scanf("%lf",&secondNumber); firstNumber = firstNumber - secondNumber; secondNumber = firstNumber + secondNumber; firstNumber = secondNumber - firstNumber; printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber); printf("After swapping, secondNumber = %.2lf", secondNumber); return 0; } // Try it now!
13th Mar 2018, 6:55 AM
H. Ahmadian
H. Ahmadian - avatar
+ 3
a,b=10,20 a=a+b b=a-b a=a-b print(a,b) # outputs 20,10 a,b=10,20 a=a*b b=a//b a=a//b print(a,b)
13th Mar 2018, 7:49 AM
Manorama
Manorama - avatar
+ 1
@Manorama in python, the best method is a, b = b, a. Your method is wrong for not integer-like objects, while the right method works for ANY pair of objects
14th Mar 2018, 10:40 AM
Amaras A
Amaras A - avatar
0
@Manorama this only works with integers or objects that are integer-like, sadly.
13th Mar 2018, 6:15 PM
Amaras A
Amaras A - avatar
0
@Amaras but using this way , we can avoid use of temporary variable.
14th Mar 2018, 6:21 AM
Manorama
Manorama - avatar