General Pseudocode Examples

Pseudocode for Swapping Two Variables7 min read

In this pseudocode we will swap the values of two variables using a temporary variable. Here, Let’s learn how to swap values of two integer variables.

“write a program that swaps the values stored/assigned in the variables x and y. you may need to define an additional variable to accomplish the task.”

Pseudocode Swap to Numbers without Entered by the User




First example, swap two variables without taking value from user.

This code defines a program in a pseudocode-like language that swaps the values of two variables, num1 and num2. The program begins by defining three variables: num1, num2, and temp. num1 and num2 are initialized with the values 10 and 15, respectively.

You may also like: Pseudocode Examples

The program then swaps the values of num1 and num2 by assigning the value of num1 to a temporary variable temp, the value of num2 to num1, and the value of temp (which was previously assigned the value of num1) to num2. Finally, the program prints the values of num1 and num2 after the swap using the OUTPUT statement.

FlowChart:

Flowchart of pseudocode to swap to variables
Flowchart of pseudocode to swap to variables

Java Code:

This code defines a Java program that swaps the values of two variables, first and second. The program begins by defining a main method, which is the entry point of the program.

Inside the main method, the program declares two float variables, first and second, and assigns them the values 2.2 and 10.4, respectively. It then prints the values of first and second to the terminal using the System.out.println statement.

The program then swaps the values of first and second using an arithmetic trick: it subtracts second from first and assigns the result to first, then adds first to second and assigns the result to second, and finally subtracts first from second and assigns the result to first.

Finally, the program prints the values of first and second again to the terminal, this time showing the swapped values.

To run this code and see the output, you can follow these steps:

  1. Save the code to a file or type it into a text editor.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where the code is saved.
  4. Run the command javac filename.java, where filename.java is the name of the file containing the code. This will compile the code and create a file called filename.class.
  5. Run the command java filename, where filename is the name of the file containing the code (without the .java extension). This will execute the program.
  6. The program will print the values of first and second before and after the swap to the terminal.

The expected output is:

–Before swap– First number = 2.2 Second number = 10.4 –After swap– First number = 10.4 Second number = 2.2

Pseudocode Swap to Numbers with Entered by the User

Second example, swap two variables with taking value from user.

This code defines a program in a pseudocode-like language that swaps the values of two variables, num1 and num2. The program begins by defining three variables: num1, num2, and temp. It then prompts the user to enter values for num1 and num2 using the INPUT statement.

The program then swaps the values of num1 and num2 by assigning the value of num1 to a temporary variable temp, the value of num2 to num1, and the value of temp (which was previously assigned the value of num1) to num2. Finally, the program prints the values of num1 and num2 after the swap using the PRINT statement.

C++ Code: Write a program to swap two numbers in C++

This code defines a C++ program that swaps the values of two variables, a and b. The program begins by including the iostream header and using the std namespace. It then defines a main function, which is the entry point of the program.

Inside the main function, the program declares three variables: a, b, and temp. It then prompts the user to enter values for a and b using the cout and cin statements, and stores these values in the variables. The program then swaps the values of a and b by assigning the value of a to a temporary variable temp, the value of b to a, and the value of temp (which was previously assigned the value of a) to b. Finally, the program prints the values of a and b after the swap to the terminal using the cout statement and the endl manipulator.

To run this code and see the output, you can follow these steps:

  1. Save the code to a file or type it into a text editor.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where the code is saved.
  4. Run the command g++ filename.cpp -o filename, where filename.cpp is the name of the file containing the code and filename is the name you want to give to the executable file.
  5. Run the command ./filename to execute the program.
  6. The program will prompt you to enter the value of a and b. Type in values for these variables and press enter.
  7. The program will then swap the values of a and b and print the results to the terminal.

For example, if you enter 10 for a and 20 for b, the output will be:

a : 20 b : 10

Python Code:Write a program to swap two numbers in Python

This code defines a program that swaps the values of two variables, x and y. The program first prompts the user to enter values for x and y using the input function. It then creates a temporary variable temp and assigns the value of x to it. The values of x and y are then swapped by assigning the value of y to x and the value of temp (which was previously assigned the value of x) to y. Finally, the program prints the values of x and y after the swap to the terminal using the print function and string formatting.

To run this code and see the output, you can follow these steps:

  1. Save the code to a file or type it into a text editor.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where the code is saved.
  4. Run the command python filename.py, where filename.py is the name of the file containing the code.
  5. The program will prompt you to enter the value of x and y. Type in values for these variables and press enter.
  6. The program will then swap the values of x and y and print the results to the terminal.

For example, if you enter 10 for x and 20 for y, the output will be:

The value of x after swapping: 20 The value of y after swapping: 10

In this example you have learned how to swap to variables using third variable with pseudoce code and programming codes.

2 Comments

Leave a Comment