Write an algorithm and flow chart to swap two numbers without using third variable
Answers
Algorithm with out using a third variable
Step 1: Start
Step 2: Read the value of Number1,Number2
Step 3: Number1= Number1 + Number2
Step 4: Number2= Number1 - Number2
Step 5: Number1= Number1 - Number2
Step 6: PRINT the value of Number1 and Number2
Step 7: Stop
Flowchart Attached below.
Answer:
Explanation:
Swapping :
Swapping is the process of altering the values of two variables interchangeably.
Algorithm:
An algorithm is a process used to carry out a computation or solve a problem. Algorithms function as a precise list of instructions that carry out defined activities in a step-by-step fashion.
Algorithm for swapping without third variable:
Step 1 : Declare two variables and
Step 2 : Get the values for the declared variables from the user
Step 3: Print the values entered by user.
Step 4 :Perform addition and subtraction to perform swapping of values as follows:
Step 4.1 :
Step 4.2 :
Step 4.3 :
Step 5 : Print the swapped values of and
#SPJ3