+ 2
Swapping of two numbers . Without temperory variable and with temperory variable. Python Program??
6 Réponses
+ 5
#1
a, b = b, a
#2
c = a
a = b
b = c
# del c
+ 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!
+ 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)
+ 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
0
@Manorama this only works with integers or objects that are integer-like, sadly.
0
@Amaras but using this way , we can avoid use of temporary variable.