C C++ program to convert binary to decimal

 

Flowchart

Binary to decimal conversion flowchart

 

Coding in C

#include<stdio.h>
int main()
{
int binary, decimal=0, digit, base=1;
printf(“Enter binary number: “);
scanf(“%d”,&binary);
while(binary!=0)
{
digit=binary%10;
decimal=digit*base+decimal;
base=base*2;
binary=binary/10;
}
printf(“Decimal number = %d”,decimal);
return 0;
}

 

Coding in C++

#include<iostream>
using namespace std;
int main()
{
int binary, decimal=0, digit, base=1;
cout<<“Enter binary number: “;
cin>>binary;
while(binary!=0)
{
digit=binary%10;
decimal=digit*base+decimal;
base=base*2;
binary=binary/10;
}
cout<<“Decimal number = “<<decimal;
return 0;
}

 

Output

Binary to decimal conversion output

 

Published by

admin

I am a computer teacher, programmer and web developer