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

8 Exciting Full Stack Coding Project Ideas &amp; Topics For Beginners
4053
A Full stack developer is an engineer who can design and develop an end-to-end application independently by handling all the work of coding, databases
Read More

by upGrad

19 Feb 2024

17 Interesting HTML Project Ideas &#038; Topics For Beginners [2024]
414954
Summary In this article, you will learn 10 Interesting HTML Project Topics. Take a glimpse below. A tribute page A survey form Technical documentati
Read More

by Rohan Vats

19 Feb 2024

How to Open json File in Excel
96592
What is JSON? JSON (JavaScript Object Notation) is a file format that is used for storing and exchanging data in the network. It is used to send data
Read More

by Rohan Vats

19 Feb 2024

JSP vs Servlet: Difference Between JSP &#038; Servlet [2024]
52597
Websites are collections of static files, for example, images, graphics, and HTML files. These websites are referred to as web applications if they pr
Read More

by Rohan Vats

19 Feb 2024

Java Developer Salary in India in 2024 [For Freshers &#038; Experienced]
900556
Wondering what is the range of Java developer salary in India? From choosing your first programming language to writing apps for Android and several
Read More

by Rohan Vats

19 Feb 2024

Polymorphism In OOPS: What is Polymorphism [Detailed Explanation]
124376
Polymorphism in OOPs is inseparable and an essential concept of every object-oriented programming language. An object or reference basically can take
Read More

by Rohan Vats

19 Feb 2024

Literals In Java: Types of Literals in Java [With Examples]
8151
Summary: In this article, you will learn about Literals in Java. Literals in Java Integral Literals Floating-Point Literals Char Literals String Lit
Read More

by Rohan Vats

19 Feb 2024

Top 30 Exception Handling Interview Questions and Answers [For Freshers &amp; Experienced]
35836
Exception handling is a concept that is implemented in algorithms to handle possible runtime errors, which may disrupt the normal flow of a program. S
Read More

by Rohan Vats

19 Feb 2024

Top 40 MySQL Interview Questions &#038; Answers For Beginners &#038; Experienced [2024]
129848
Have a Data engineering or data science interview coming up? Need to practice some of the most asked MySQL interview questions? The article compiles t
Read More

by Rohan Vats

19 Feb 2024

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