Home / Programs / Using Command Line Arguments Program to Convert a Decimal to Binary Number
Programming Example

Using Command Line Arguments Program to Convert a Decimal to Binary Number

👁 1,105 Views
💻 Practical Program
📘 Step by Step Learning
Convert a Decimal to Binary Number Using Command-Line Arguments Program to

Program Code

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

}

Output

10

1010

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.