Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconBinary To Decimal C++: Program to Convert Binary to Decimal

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

Last updated:
2nd May, 2021
Views
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
Binary To Decimal C++: Program to Convert Binary to Decimal

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.

Ads of upGrad blog

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.

Explore Our Software Development Free Courses

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 

Explore our Popular Software Engineering Courses

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 20int main( )
Step 21{
Step 22string num = “10101000”;
Step 23cout << 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?

In-Demand Software Development Skills

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 20int n = 18;
Step 21decToBinary (n);
Step 22return 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.

Ads of upGrad blog

Read our Popular Articles related to Software Development

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.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What 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.

2Which 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.

3What 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++.

Explore Free Courses

Suggested Blogs

Full Stack Developer Salary in India in 2024 [For Freshers &#038; Experienced]
907174
Wondering what is the range of Full Stack Developer salary in India? Choosing a career in the tech sector can be tricky. You wouldn’t want to choose
Read More

by Rohan Vats

15 Jul 2024

SQL Developer Salary in India 2024 [For Freshers &#038; Experienced]
902298
They use high-traffic websites for banks and IRCTC without realizing that thousands of queries raised simultaneously from different locations are hand
Read More

by Rohan Vats

15 Jul 2024

Library Management System Project in Java [Comprehensive Guide]
46958
Library Management Systems are a great way to monitor books, add them, update information in it, search for the suitable one, issue it, and return it
Read More

by Rohan Vats

15 Jul 2024

Bitwise Operators in C [With Coding Examples]
51783
Introduction Operators are essential components of every programming language. They are the symbols that are used to achieve certain logical, mathema
Read More

by Rohan Vats

15 Jul 2024

ReactJS Developer Salary in India in 2024 [For Freshers &#038; Experienced]
902674
Hey! So you want to become a React.JS Developer?  The world has seen an enormous rise in the growth of frontend web technologies, which includes web a
Read More

by Rohan Vats

15 Jul 2024

Password Validation in JavaScript [Step by Step Setup Explained]
48976
Any website that supports authentication and authorization always asks for a username and password through login. If not so, the user needs to registe
Read More

by Rohan Vats

13 Jul 2024

Memory Allocation in Java: Everything You Need To Know in 2024-25
74112
Starting with Java development, it’s essential to understand memory allocation in Java for optimizing application performance and ensuring effic
Read More

by Rohan Vats

12 Jul 2024

Selenium Projects with Eclipse Samples in 2024
43494
Selenium is among the prominent technologies in the automation section of web testing. By using Selenium properly, you can make your testing process q
Read More

by Rohan Vats

10 Jul 2024

Top 10 Skills to Become a Full-Stack Developer in 2024
230001
In the modern world, if we talk about professional versatility, there’s no one better than a Full Stack Developer to represent the term “versatile.” W
Read More

by Rohan Vats

10 Jul 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon