Programs

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

Introduction

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. Coding in C++ primarily involves using functions, classes, processing strings and arrays of data input and output. 

Decimal numbers are the common integers that we use in our daily lives. The decimal numbers have a base of 10, as 10 possible digits are ranging from 0,1,2 … 9. Decimal numbers are convenient for usage in our daily lives, but a machine/computer cannot directly process decimal numbers through its CPU. Binary numbers are a set of 0s and 1s that can be processed by the computer system, as it forms the machine language.

These numbers are arranged in unique configurations that correlate to numbers, letters, special characters, or other fundamental information. The base of these binary numbers is ‘2’, as the only digits it contains are 0 and 1. For eg. 0010 (Binary number) = 2 (Decimal number).

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.

Explore Our Software Development Free Courses

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 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 number into a decimal number in the C++ programming interface by utilising 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

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.

Read our Popular Articles related to Software Development

Conclusion

These C++ programming codes are some of the elementary programs taught in the introduction sections of programming courses. upGrad hosts a range of courses for the students who want to understand programming in-depth.

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.

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.

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.

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

Want to share this article?

Land on Your Dream Job

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks