Programming Example
Write a program to swap two numbers using command line programming
A program to swap two numbers using command line programming
// 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;
}
10 12
After swapping, firstNumber = 12.00
After swapping, secondNumber = 10.00
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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.