Programming Example
Using Command Line Arguments Program to Convert a Decimal to Binary Number
Convert a Decimal to Binary Number Using Command-Line Arguments Program to
#include<stdio.h>
#include<stdlib.h>
int main (int argc, char *argv[])
{
if (argc == 1)
{
printf ("No Arguments ");
return 0;
}
else
{
int n;
n = atoi (argv[1]);
int binaryN[64];
int i = 0;
int j;
while (n > 0)
{
//storing in binary array remainder of number
binaryN[i] = n % 2;
n = n / 2;
i++;
}
//printing reverse array
while (i)
{
printf ("%d", binaryN[--i]);
}
return 0;
}
}
10
1010
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.