Home »
Code Examples »
Scala Code Examples
Scala - Program to swap two numbers without using third variable Code Example
The code for Program to swap two numbers without using third variable
// Scala program to swap two numbers
// without using 3rd variable
object Sample{
def main(args:Array[String]){
var num1:Int = 10;
var num2:Int = 20;
println("Numbers before swapping:")
printf("\tNum1:%d\n",num1)
printf("\tNum2:%d\n",num2)
num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2
println("Numbers after swapping:")
printf("\tNum1:%d\n",num1)
printf("\tNum2:%d\n",num2)
}
}
/*
Output:
Numbers before swapping:
Num1:10
Num2:20
Numbers after swapping:
Num1:20
Num2:10
*/
Code by IncludeHelp,
on August 12, 2022 23:01