Get 20% Off + $20 Signup Bonus ! Limited Time, Hurry !
Get 20% Off + $20 Signup Bonus ! Limited Time, Hurry !
Securing Higher Grades Costing Your Pocket? Book Your Assignment at The Lowest Price Now!
loader
Add File

Error goes here

Files Missing!

Please upload all relevant files for quick & complete assistance.

Save Time & Excel Academics

Share your requirements & connect with top tutor !

watch
question
We will use e-mail only for:

arrow Communication regarding your orders

arrow To send you invoices, and other billing info

arrow To provide you with information of offers and other benefits

Add File

Files Missing!

Please upload all relevant files for quick & complete assistance.

2,131,439

Orders

4.9/5

Overall Rating

5,205

Experts

C Program to Convert Binary Number to Decimal and Vice-Versa

UserJack Williams time29 July,2022

In this example, you will learn to convert binary numbers to decimal and vice-versa manually by creating a user-defined function.

In order to understand this specific example, one must have knowledge regarding the following C programming topics:

  • C Functions
  • C User-defined functions

Example 1: C Program to Convert Binary Number to Decimal

// convert binary to decimal

#include <stdio.h>

#include <math.h>

// function prototype

int convert(long long);

int main() {

  long long n;

  printf("Enter a binary number: ");

  scanf("%lld", &n);

  printf("%lld in binary = %d in decimal", n, convert(n));

  return 0;

}

// function definition

int convert(long long n) {

  int dec = 0, i = 0, rem;

  while (n!=0) {

    rem = n % 10;

    n /= 10;

    dec += rem * pow(2, i);

    ++i;

  }

  return dec;

}

Run Code

Output

Enter a binary number: 1101

1101 in binary = 13 in decimal

In the programming Languages courses, we have included the header file math. h to perform mathematical operations in the program.

We ask the user to enter a binary number and pass it to the convert() function to convert it decimally.

Suppose n = 1101. Let's see how the while loop in the convert() function works.

n != 0rem = n % 10n /= 10idec += rem * pow(2, i)
1101 != 01101 % 10 = 11101 / 10 = 11000 + 1 * pow (2, 0) = 1
110 != 0110 % 10 = 0110 / 10 = 1111 + 0 * pow (2, 1) = 1
10 != 011 % 10 = 111 /10 = 121 + 1 * pow (2, 2) = 5
1 != 01 % 10 = 11 / 10 = 035 + 1 * pow (2, 3) = 13
0 != 0---Loop terminates

So, 1101 in binary is 13 in decimal.

Now, let's see how we can change the decimal number into a binary number.


Example 2: C Program to convert decimal numbers to binary

// convert decimal to binary

#include <stdio.h>

#include <math.h>

long long convert(int);

int main() {

  int n, bin;

  printf("Enter a decimal number: ");

  scanf("%d", &n);

  bin = convert(n);

  printf("%d in decimal =  %lld in binary", n, bin);

  return 0;

}

long long convert(int n) {

  long long bin = 0;

  int rem, i = 1;

  while (n!=0) {

    rem = n % 2;

    n /= 2;

    bin += rem * i;

    i *= 10;

  }

  return bin;

}

Run Code

Output

Enter any decimal number: 13

13 in the decimal = 1101 in binary

Suppose n = 13. One can see the manner in which while loop in convert () function works.

n != 0rem = n % 2n /= 2ibin += rem * ii * = 10
13 != 013 % 2 = 113 / 2 = 610 + 1 * 1 = 11 * 10 = 10
6 != 06 % 2 = 06 / 2 = 3101 + 0 * 10 = 110 * 10 = 100
3 != 03 % 2 = 13 / 2 = 11001 + 1 * 100 = 101100 * 10 = 1000
1 != 01 % 2 = 11 / 2 = 01000101 + 1 * 1000 = 11011000 * 10 = 10000
0 != 0-- -Loop terminates

Therefore, 13 in the decimal is 1101 in the binary.

Keyword w- Binary to Decimal

1) Binary to Decimal in c++

Provided with any particular binary number as a form of input, one shall be required to write any specific program in order to convert the provided binary number into any equivalent decimal number.

Examples: 

Input: 111

Output: 7

Input: 1010

Output: 10

Input: 100001

Output: 33

The idea can be said to actually extract the digits of any given binary number initial from the specific rightmost digit as well as keep any variable dec_value. During the time of extracting digits from the specific binary number, one should multiply the specific digit with the genuine or proper base (Power of 2) as well as add it to the specific variable dec_value. At the end of the conclusion, the specific variable dec_value will actually store the mandated decimal number.

For Example:

If the specific binary number is 111.

dec_value = 1*(2^2) + 1*(2^1) + 1*(2^0) = 7

2) Decimal to Binary Conversion Algorithm

C Program to convert the Decimal into Binary

Decimal to the binary in the C: One shall be able to convert any specific decimal number (base-10 (0 to 9)) into the binary number (base-2 (0 or 1)) by the c program.

Decimal Number

A decimal number can be said to be a base 10 number as it ranges from the number 0 to the number 9, there can be said to be a total of 10 digits from 0 to 9. Any specific combination of the digits can be said to be a decimal number such as 23, 445, 132, 0, 2, and others.

Binary Number

The Binary number can be said to be a base 2 number as it is either the number 0 or the number 1. Any specific combination of the number 0 and the number 1 can be said to be binary numbers such as 1001, 101, 11111, 101010, and others.

One can certainly see the few binary numbers for the specific decimal number.

3) 5 program examples or instances to convert the decimal to the binary

a) The decimal to the binary utilizing while the loop

Program 1

Explanation (in points)

b) the decimal to the binary utilizing while without the loop

c)Program 1

Explanation (in points)

4) the decimal to the binary utilizing array

Program 1

Explanation (in points)

  1. The decimal to the binary without utilizing an array

Program 1

Explanation (in points)

B) the decimal to the binary without utilizing a stack

Program 1

Explanation (in points)

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 said specific C++ program shall effectively convert any specific binary number (up to 31) into any decimal number. In order to convert the specific binary numbers larger than 31 into the particular decimal numbers, a string shall be required to be initialized, along with the while loop.

Related Posts

comment 0 Comments

Leave a Reply

Your email address will not be published.

Save Time & Excel Academics

Share your requirements & connect with top tutor !.

watch
question
We will use e-mail only for:

arrow Communication regarding your orders

arrow To send you invoices, and other billing info

arrow To provide you with information of offers and other benefits

loader
Add File

Files Missing!

Please upload all relevant files for quick & complete assistance.

2,131,439

Orders

4.9/5

Overall Rating

5,205

Experts

Still in Two Minds? The Proof is in Numbers!

Genuine Reviews With a Rating of 4.9/5.

student rating student rating student rating student rating student rating
29 Nov 2022

Marketing

Assignment: 3 Pages, Deadline: 23 days

Did a great job! Met all requirements, really appreciate the work that was put in, thanks again!

flag User ID: 9***72 United States

student rating student rating student rating student rating student rating
28 Nov 2022

Music

Assignment: 1 Page, Deadline: 5 hours

my essay was done on time and done correctly highly recommend again. thank you so much again.

flag User ID: 4***24 United States

student rating student rating student rating student rating student rating
28 Nov 2022

Sociology

Home Work: 1 Page, Deadline: 10 hours

thank you, the work was excellent, and I was able to get a great score on my paper thank you.

flag User ID: 4***24 United States

student rating student rating student rating student rating student rating
28 Nov 2022

Healthcare

Essay: 4 Pages, Deadline: 11 hours

thank you for my assignment the work was great and completed on time! highly recommended they get the job done.

flag User ID: 4***24 United States

student rating student rating student rating student rating student rating
28 Nov 2022

Management

Assignment: 2 Pages, Deadline: 6 days

The writer was able to submit the assignment on time and with great content. Will order again.

flag User ID: 2***30 Canada

student rating student rating student rating student rating student rating
28 Nov 2022

HRM

Assignment: 12 Pages, Deadline: 9 days

Thank you for your time and effort. I appreciate you have managed to complete it the assignment in that short time.

flag User ID: 8***69 United Arab Emirates

student rating student rating student rating student rating student rating
28 Nov 2022

Statistics

Programming: 1 Page, Deadline: 14 hours

I told them late about my assignment and they completed it before time and the customer services is really great they even tell everything about the s ...

flag User ID: 1***570 Canada

student rating student rating student rating student rating student rating
28 Nov 2022

Operations

Programming: 2 Pages, Deadline: 14 hours

good work overall. the work fast and efficient. i would recommend for last minute work

flag User ID: 3***05 Canada

student rating student rating student rating student rating student rating
28 Nov 2022

Accounting

Assignment: 10 Pages, Deadline: 11 days

i would like to thank you for the expert efforts in this work all answers meet the question requirement thanks

flag User ID: 3***56 Qatar

student rating student rating student rating student rating student rating
28 Nov 2022

Management

Essay: 24 Pages, Deadline: 22 days

I am pleased with the exert work and want to keep him for the remainder of the program. However, I want the expert to provide solutions that will limi ...

flag User ID: 7***33 United States

student rating student rating student rating student rating student rating
28 Nov 2022

HRM

Thesis: 1 Page, Deadline: 6 days

I got a perfect score on this assignment. The assignment had everything it needed plus more. I will use expert again.

flag User ID: 5***56 United States

student rating student rating student rating student rating student rating
28 Nov 2022

HRM

Essay: 1.6 Pages, Deadline: 3 days

Expert was great at this assignment. Thank you for yout help, I got an A on this assignment. Will use expert again.

flag User ID: 5***56 United States

student rating student rating student rating student rating student rating
28 Nov 2022

Marketing

Assignment: 6 Pages, Deadline: 3 days

Evening Sir.Just tell you has done an excellent job: an outstanding presentation and well-defined and explained search material. I am pleased with the ...

flag User ID: 9***77 Great Britain

student rating student rating student rating student rating student rating
28 Nov 2022

Management

Assignment: 8 Pages, Deadline: 9 days

very happy and the quality of paper was good and got a very satisfied marks and the customer service is very excellent.

flag User ID: 2***37 Australia

student rating student rating student rating student rating student rating
28 Nov 2022

It Write Up

Assignment: 6 Pages, Deadline: 5 days

very happy and got a very satisfied marks and the customer service is very excellent.

flag User ID: 2***37 Australia

student rating student rating student rating student rating student rating
28 Nov 2022

Management

Essay: 1.4 Pages, Deadline: 3 days

achieved a very good and satisfied marks. really happy with my grade and the customer service is very excellent

flag User ID: 2***37 Australia

student rating student rating student rating student rating student rating
28 Nov 2022

Economics

Assignment: 2 Pages, Deadline: 5 days

The expert did a great job writing my essay. All requirements that were given, he wrote it in a good way.

flag User ID: 1***967 Netherlands

student rating student rating student rating student rating student rating
28 Nov 2022

Nursing

Programming: 2 Pages, Deadline: 1 day

Hello, you have done a previous assessment for the same unit about monkey pox in the USA, but this one you have made is about malaria. Both assessment ...

flag User ID: 7***82 Australia

student rating student rating student rating student rating student rating
28 Nov 2022

HRM

Essay: 1 Page, Deadline: 20 hours

The work was done properly and submitted of time. The expert touch all key issues that was needed to be touch. Thanks for a great work .

flag User ID: 1***76 Sweden

student rating student rating student rating student rating student rating
28 Nov 2022

Property Law

Essay: 2 Pages, Deadline: 3 days

Great write up - expert attended to all requested requirements for the task. Thank you!

flag User ID: 4***46 Australia

icon

5% Cashback

On APP - grab it while it lasts!

Download app now (or) Scan the QR code

*Offer eligible for first 3 orders ordered through app!

screener
ribbon
Have any Query?