VOOZH about

URL: https://www.geeksforgeeks.org/interview-experiences/tcs-coding-practice-question-swap-2-numbers/

⇱ TCS Coding Practice Question | Swap two Numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TCS Coding Practice Question | Swap two Numbers

Last Updated : 11 Jul, 2025

Given two numbers, the task is to swap the two numbers using Command Line Arguments. 👁 Image
Examples:

Input: n1 = 10, n2 = 20
Output: 20 10

Input: n1 = 100, n2 = 101
Output: 101 100

Approach:

  • Since the numbers are entered as Command line Arguments, there is no need for a dedicated input line
  • Extract the input numbers from the command line argument
  • This extracted numbers will be in string type.
  • Convert these numbers into integer type and store it in variables, say num1 and num2
  • Get the sum in one of the two given numbers.
  • The numbers can then be swapped using the sum and subtraction from the sum.

Program: 

Output:

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment