Command Line Programming Question to Cover Binary to Decimal Number

Write a c program to convert binary to decimal using command line arguments?

Please provide the solution for this in command line arguments. The answer will be added from the comments.

Here is basic C program without command line –

[code language=”cpp”] 
#include<stdio.h>
int main(int argc, char *argv[]){
int num,binary,decimal=0,rem,base=1;
num=atoi(argv[1]);
binary=num;
while(num>0){
rem=num%2;
decimal+=rem*base;
num=num/10;
base=base*2;
}
printf("%d",decimal);
return 0;
}
[/code]

 

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

2 comments on “Command Line Programming Question to Cover Binary to Decimal Number”


  • Moumita Bhattacharjee

    // Program to print binary to decimal value of a given number
    // command line argument
    // once we get the value from command
    // line we can use them to solve our problem
    #include
    #include
    #include

    int main(int argc, char *argv[])
    {
    int bin1,j=0,rem,dectobin=0;

    if(argc==1)
    {
    printf(“only present default value ,Sorry we can not do any performance \”\n”);
    return -1;
    }
    else{

    bin1=atoi(argv[1]);
    while (bin1!=0)
    {
    rem = bin1%10;
    bin1/= 10;
    dectobin += rem*pow(2,j);
    ++j;
    }
    printf(“%d”,dectobin);
    }
    return 0;
    }