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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | BEGIN #define variables and assing values to variables NUMBER num1=10, num2=15, temp #assign num1 to temp temp=num1 #assing num2 to num1 num1=num2 #assign temp to num2 num2=temp OUTPUT "number 1:"+num1 OUTPUT "number 2:"+num2 END |
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:
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static void main(String[] args) { float first = 2.2f, second = 10.4f; System.out.println("--Before swap--"); System.out.println("First number = " + first); System.out.println("Second number = " + second); first = first - second; second = first + second; first = second - first; System.out.println("--After swap--"); System.out.println("First number = " + first); System.out.println("Second number = " + second); } |
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:
- Save the code to a file or type it into a text editor.
- Open a terminal or command prompt.
- Navigate to the directory where the code is saved.
- Run the command
javac filename.java
, wherefilename.java
is the name of the file containing the code. This will compile the code and create a file calledfilename.class
. - Run the command
java filename
, wherefilename
is the name of the file containing the code (without the.java
extension). This will execute the program. - The program will print the values of
first
andsecond
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | BEGIN #define variables and assing values to variables NUMBER num1, num2, temp #take numbers from user INPUT num1 INPUT num2 #assign num1 to temp temp=num1 #assing num2 to num1 num1=num2 #assign temp to num2 num2=temp2 PRINT "number 1:"+num1 PRINT "number 2:"+num2 END |
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++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using namespace std; #include<iostream> int main() { int a,b,temp; cout<<"Enter the value of a: "; cin>>a; cout<<"Enter the value of b : "; cin>>b; temp = a; a = b; b = temp; cout<<"a : "<<a<<endl; cout<<"b : "<<b<<endl; cout << "Press a key to continue ..." << endl; cin.ignore(); cin.get(); return EXIT_SUCCESS; } |
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:
- Save the code to a file or type it into a text editor.
- Open a terminal or command prompt.
- Navigate to the directory where the code is saved.
- Run the command
g++ filename.cpp -o filename
, wherefilename.cpp
is the name of the file containing the code andfilename
is the name you want to give to the executable file. - Run the command
./filename
to execute the program. - The program will prompt you to enter the value of
a
andb
. Type in values for these variables and press enter. - The program will then swap the values of
a
andb
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # Python program to swap two variables # To take input from the user x = input('Enter value of x: ') y = input('Enter value of y: ') # create a temporary variable and swap the values temp = x x = y y = temp print('The value of x after swapping: {}'.format(x)) print('The value of y after swapping: {}'.format(y)) |
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:
- Save the code to a file or type it into a text editor.
- Open a terminal or command prompt.
- Navigate to the directory where the code is saved.
- Run the command
python filename.py
, wherefilename.py
is the name of the file containing the code. - The program will prompt you to enter the value of
x
andy
. Type in values for these variables and press enter. - The program will then swap the values of
x
andy
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.
[…] This link to see the sample(s) […]
Nice post plz share more post