Tuesday, 21 February 2017

Swap Numbers

QUESTION DESCRIPTION :

Write a C Program to swap two variables without using third or temp variable


TEST CASE 1 

INPUT

20
5

OUTPUT

Values after Swapping
value of a is:5
value of b is:20


EXPLANATION : 

The code requires you to receive an input of two numbers , swap them and display the results ( Different Swapping Methods ). I have used the mathematical approach.


ALGORITHM :

  • Take two inputs from the user ( a,b )
  • Add second value to the first value and store it in the first variable ( a=a+b )
  • To add new value to second variable , subtract the value of second variable from the value of the first variable and store it in the second one ( b=a-b )
  • Perform a similar operation on the first variable ( a=a-b )
  • The numbers are swapped , print them with the required output message.

SOLUTION : (Please use this as reference only)

CODE


No comments:

Post a Comment