Simple Algorithm To Swap Two Numbers

Princy Prakash
1 min readApr 17, 2023

--

ALGORITHM:

  1. Get input from the user for n1 and n2.
  2. Store the value of n1 in a temporary variable, let’s call it n3.
  3. Set n1 to the value of n2.
  4. Set n2 to the value of n3.
  5. Print the new values of n1 and n2.

In Detailed Explanation:

Step 1:Get the input.

Get Input from User To swap two numbers, we need to first get the input from the user.

Step 2–4: Swap the Numbers.

To swap the numbers, we’ll use a temporary variable to hold the value of n1 while we reassign it to n2. We’ll then set n2 to the original value of n1, which is now stored in our temporary variable.

Step 5: Print the Result

Now that we’ve swapped the numbers, we can print the new values of n1 and n2 to confirm that the swap was successful.

Conclusion:

By following the algorithm outlined in this article, even beginners can learn how to swap numbers with ease.

FLOWCHART:

--

--

Princy Prakash
Princy Prakash

Written by Princy Prakash

Hi I Am Princy. I recently discovered my passion for programming and excited to get deeper into the world of technologies also in problem-solving

No responses yet