Binary number to decimal number

PROBLEM :

Given a Binary Number, Print its decimal equivalent.


Input:

The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case contains a single Binary number.


Output:

Print each Decimal number in new line.


Constraints:

1< T <100
1<=Digits in Binary<=8


Example:

1
10001000
136

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<math.h>
int decimal_number(int ) ;
int main()
 {
int t,no ;
cin>>t ;
while(t--)
{
   cin>>no ;
   cout<<decimal_number(no)<<endl ;
}
return 0;
}

int decimal_number(int no)
{
    int i,sum,r ;
    i=0 ;
    sum=0 ;
   
    while(no)
    {
        r=no%10 ;
        no=no/10 ;
        sum=sum+r*pow(2,i) ;
        i++ ;
    }
    return sum ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )