Decimal to Binary

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

It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later.

[code language=”cpp”]

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

[/code]

Check all other command Line Programs for TCS here on our Command line Programming Dashboard

One comment on “Decimal to Binary”