Program in C++ to Convert Binary to Decimal number Using Function
First Method
//https://clanguage0.blogspot.com #include<iostream> #include<conio.h> #include<string.h> using namespace std; int BinToDec(char bin[]) { int dec=0,base=1,temp,len,i=0; for(i=strlen(bin)-1;i>=0;i--) { temp=int(bin[i])-48; dec=temp*base+dec; base*=2; } return dec; } int main() { char bin[7]; cout<<"Ener a binary No.:-"; cin>>bin; int bn=BinToDec(bin); cout<<"Decimal No.="<<bn; getch(); }
Output:-
Second Method
#include<iostream> #include<conio.h> #include<string.h> using namespace std; int BinToDec(int bin) { int dec,base=1; for(dec=0;bin!=0;bin/=10) { dec=(bin%10)*base+dec; base*=2; } return dec; } int main() { int bin; cout<<"Enter a binary No.:-"; cin>>bin; int bn=BinToDec(bin); cout<<"Decimal No.="<<bn; getch(); }
Output:-
0 Comments