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

🖥 Program Output

10 12


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


                            

📘 Explanation

Nope
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.