Home / Programs / Write a program to swap two numbers using command line programming
Programming Example

Write a program to swap two numbers using command line programming

👁 3,456 Views
💻 Practical Program
📘 Step by Step Learning
A program to swap two numbers using command line programming

Program Code

// Program by atnyla
// A program to swap two numbers using command line programming

#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;
}

Output

10 12


After swapping, firstNumber = 12.00
After swapping, secondNumber = 10.00

Explanation

Nope

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.