160k views
5 votes
A. Design an algorithm that takes 2 integers input from users in 2 variables, without using any other variable swap the values. Design its flow chart as well.

User Aviv Paz
by
3.4k points

1 Answer

5 votes

Answer:

The algorithm is as follows

  1. Start
  2. Accept Input1 and Input2
  3. Input1 = Input1 + Input2
  4. Input2= Input1- Input2
  5. Input1= Input1- Input2
  6. Print Input1
  7. Print Input2
  8. Stop

See attachment for Flowchart

Line 1 and 8 of the algorithm starts and stops the algorithm

Line 2 accepts the two integer variables (input1 and input2)

Take for instance input1 = 5 and input2 = 4

Line 3 increases input1 by input2

At this point, Input1 = 5 + 4 = 9

Line 4 subtracts input2 from input1 and saves it in input2

i.e. Input2 = 9 - 4 = 5

Note that, the value of input2 has changed to the initial value of Input1

Line 5 subtracts input2 from input1 and saves it in input1

i.e. Input1 = 9 - 5 = 4

Note that, the value of input1 has changed to the initial value of Input2

Line 6 and 7 prints the new values of input1 and input2

The flowchart does the same thing as the algorithm; the only difference is that it uses lines and shapes to depict flow of program

The flowchart was designed using Microsoft Office Word

A. Design an algorithm that takes 2 integers input from users in 2 variables, without-example-1
User Gnarfoz
by
4.1k points