Command Line Program for Swapping two Numbers

Ques. Write a program to swap two numbers using command line programming?

It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later.

[code language=”cpp”]

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main(int argc, char * argv[])
{
if(argc==1){
printf("No command line argument present, add them first");
return 0;
}

double firstNumber, secondNumber, temporaryVariable;
 firstNumber = atoi(argv[1]);

 secondNumber = atoi(argv[2]);

 temporaryVariable = firstNumber;

 firstNumber = secondNumber;

 secondNumber = temporaryVariable;

 printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);

 printf("After swapping, secondNumber = %.2lf", secondNumber);

return 0;
}

[/code]

Other TCS Coding Questions –

One comment on “Command Line Program for Swapping two Numbers”