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 ;
}
---------------------------------------------------------------------------------
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
Post a Comment