Swap Two Numbers without Temp Variable in PHP
Q. Write a PHP program to swap two numbers without using a temp variable
<?php // Define two numbers $a = 10; $b = 20; // Display before swapping echo "Before swapping: a = $a, b = $b<br>"; // Swap without temp variable $a = $a + $b; $b = $a - $b; $a = $a - $b; // Display after swapping echo "After swapping: a = $a, b = $b"; ?>
Q. Write an algorithm to swap two numbers without using a temp variable in PHP
1. Start 2. Define two numbers: a and b 3. Add a and b, store in a: a = a + b 4. Subtract new a by b, store in b: b = a - b 5. Subtract new a by new b, store in a: a = a - b 6. Print the swapped values 7. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com