Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Binary To Decimal C++: Program to Convert Binary to Decimal

Updated on 12 February, 2024

19.61K+ views
8 min read

Throughout my career as a software developer, I’ve encountered numerous challenges that required me to dive deep into the nuances of programming languages, especially C++. One such challenge is converting binary numbers to decimal format, a task that might seem straightforward at first but is crucial for understanding the interaction between different data representations in computer systems. This experience led me to explore various methods and best practices for implementing this conversion, specifically focusing on Binary To Decimal C++ conversions.

In this article, I aim to share my insights and knowledge on this topic, guiding aspiring professionals through the intricacies of binary to decimal conversion using C++. C++ is one of the principal programming languages as it is the necessary step to learn how to ‘code’. Students are expected to learn C++ thoroughly before other programming languages such as Java, CSS, etc., are introduced through their curriculum.

We’ll start with the basics—what binary and decimal numbers are—before moving on to practical C++ programs designed to perform this conversion efficiently.

Check out our free courses to get an edge over the competition.

When the computer performs calculations within its system, the binary number thus calculated needs to be displayed to the user through a decimal number. For this purpose, a C++ program that can convert a binary number into a decimal number is used, as displayed below.

What is a Binary Number? 

A binary number is expressed in the base-2 numeral system, which uses only two symbols: typically, 0 (zero) and 1 (one). Each digit in a binary number represents a power of 2, with the rightmost digit representing 2^0, the next 2^1, and so on. Converting these numbers into a more familiar base-10 system, or decimal, is a common task in programming. For example, using binary to decimal C++ code allows programmers to convert binary numbers into decimal numbers easily, facilitating interactions between the binary logic of computers and the decimal-based understanding of humans. 

Binary Numbers can be understood by the below mentioned characteristics. 

  • Base-2 System: Binary numbers belong to the base-2 numeral system, unlike our everyday base-10 system. This means they only use two digits – 0 and 1. 
  • Building Blocks: Each digit in a binary number is called a bit. It’s the smallest unit of data in computing. 
  • Powers of 2: Binary counting follows powers of 2. The rightmost bit represents 2^0, the next 2^1, then 2^2, and so on, doubling with each position. 
  • Digital Language: Computers use binary code for their language, with combinations of 0s and 1s representing all data and instructions. 
  • Data Storage: Binary is fundamental in data storage and processing, with bits forming the basis for encoding information in a computer’s memory and communication systems. 

What is Decimal Number?

It’s a way of representing numbers that includes a decimal point to separate the whole from the fractional part. This system is commonly used in daily life for measuring, counting, and various calculations, making it easy to understand and apply. 

Decimal numbers have the following characteristics: 

  • Base-10 System: Decimal numbers belong to the base-10 numeral system, the system most commonly used by humans for everyday counting. 
  • Ten Digits: The decimal system has ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. 
  • Place Value System: Each digit’s value depends on its position in a number. Moving left increases the value by powers of 10. 
  • Whole and Fractional Parts: Decimal numbers can represent both whole numbers and fractions. The dot (.) separates the whole part from the fractional part. 
  • Human-Friendly: Decimal numbers align with human counting habits, making them practical for daily activities like shopping, calculations, and communication. 

Programs to Convert Binary to Decimal in C++

C++ Program 1

Step 1.  #include<iostream.h>
Step 2.  using namespace std;
Step 3.  int main()
Step 4. {
Step 5.  int num, temp, rem, dec = 0, b = 1; 
Step 6. cout << “Enter Binary Number – ”;
Step 7. cin >> num
Step 8. temp >> num
Step 9. while (num > 0)
Step 10. {
Step 11. rem = temp % 10
Step 12. dec = dec + rem * 2
Step 13. b =*2;
Step 14. temp /= 10;
Step 15. }
Step 16. cout << “The decimal conversion of “ << num << “is” << dec;
Step 17. return 0;
Step 18.  }

The above C++ program will effectively convert a binary number (up to 31) into a decimal number. To convert binary numbers larger than 31 into decimal numbers, a string needs to be initialized, along with the while loop.

Check out upGrad’s Advanced Certification in DevOps 

C++ Program 2

Step 1.  #include<iostream.h>
Step 2.  #include<string.h>
Step 3.  using namespace std;
Step 4. int binarytodecimal(string n)
Step 5.  {
Step 6. string num = n;
Step 7. int dec_value = 0;
Step 8. int base = 1;
Step 9. int len = num.length( );
Step 10. for (int i = len – 1; i >= 0; i–)
Step 11. {
Step 12. while (num > 0)
Step 13. {
Step 14.   if (num[i] == ‘1’)
Step 15. dec_value += base;
Step 16. base = base * 2;
Step 17. }
Step 18.  return dec_value;
Step 19 }
Step 20 int main( )
Step 21 {
Step 22 string num = “10101000”;
Step 23 cout << binarytodecimal(num) << endl;
Step 24 }

The output of the above code will be displayed as follows: “168”.

Thus, we can convert a binary to decimal C++ programming interface by utilizing the code in two different methods. Application of the given C++ program includes the display of a decimal number on the computer screen after the ALU performs mathematical calculations, as requested by the user. Since the computer processes data in ‘bits’, as series of Ones and Zeros, the processed data must be converted into decimal numbers for the user’s understanding and comprehension.

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

Check out upGrad’s Advanced Certification in Cyber Security

To convert decimal numbers into binary numbers in the C++ programming interface, the following code must be used:

Also Read: C++ Project Ideas & Topics

C++ Program 3

This is the third program to convert binary to decimal in C++.

Step 1.  #include<iostream.h>
Step 2.  using namespace std;
Step 3.  void decToBinary (int n)
Step 4. {
Step 5.  int binaryNu[32];
Step 6. int i = 0;
Step 7. while (n > 0)
Step 8. {
Step 9. binaryNum[ i ] = n % 2;
Step 10. n = n / 2;
Step 11. i++;
Step 12. }
Step 13. for 
Step 14. int (j = i – 1, j > 0; j — )
Step 15. cout << binaryNum [ j ];
Step 16. base = base * 2;
Step 17. }
Step 18.  int main ( )
Step 19 {
Step 20 int n = 18;
Step 21 decToBinary (n);
Step 22 return 0;
Step 23 }

The output of the above code will be displayed as “10010”, for an entered input of “18”. 

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Conclusion

In conclusion, the journey from understanding the basics of binary and decimal numbers to mastering Binary To Decimal C++ conversion through the implementation of various C++ programs is both enriching and essential for any programmer. The transition from binary to decimal systems is not just a fundamental concept in computer science but a crucial skill in the arsenal of developers, enabling them to bridge the gap between machine-level and human-understandable data. Through the detailed exposition of C++ Program 1, C++ Program 2, and C++ Program 3, we have delved into the practical aspects of this conversion, offering readers a comprehensive guide to tackling similar challenges with confidence and proficiency. By embedding these exact strategies and understanding the underlying principles, one can enhance their programming toolkit, paving the way for more complex and innovative applications in the future.

If you are interested to learn more and need mentorship from industry experts, check out upGrad & IIIT Banglore’s Executive PG Programme in Full-Stack Software Development.

Frequently Asked Questions (FAQs)

1. What are the different datatypes in C++?

C++ program defines data types in order to store data in memory in a certain format. Data type is the set of values, symbols and methods used to represent information in a computer memory. Data types make it possible to group common structures and functions. C++ language supports all data types: scalar types, SIMD types, user-defined types and incomplete types. The C++ language has five basic data types: char, int, float, double and bool. char represents a single character, bool is a logical value that can be either true or false, int represents a whole number and float is a floating-point number.

2. Which is better - C++ or Java?

C++ is generally easier to learn and has a lot of power. C++ is generally easier to learn and has a lot of power. It is basically a case of what kind of software you want to develop. If you want to develop very large and complex software C++ is the better choice. If your applications are smaller, you want to develop them quickly, or you want them to run on the internet, then Java is the better choice.

3. What are the applications of C++?

C++ is a widely used computer programming language. It's used to build more and more software and games. The language has been extended over the years to support mobile devices, to support multiple platforms and to provide better support to the programmer. C++ is used to build desktop applications, which are complex software used by companies to help run the devices and systems they have in place. It's also used to build web applications, which are software designed to be accessed by online users through a web browser. C++ is also used to build mobile applications, which are software used to run on a mobile device like a smartphone or a tablet. Any device that has a screen, memory, and a processor can be programmed with C++.

RELATED PROGRAMS