Get 20% Off + \$20 Signup Bonus ! Limited Time, Hurry !

### Save Time & Excel Academics

Share your requirements & connect with top tutor !

Orders

Overall Rating

Experts

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

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

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.

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

## 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

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.

### Save Time & Excel Academics

Share your requirements & connect with top tutor !.

Orders

Overall Rating

Experts

## Still in Two Minds? The Proof is in Numbers!

Genuine Reviews With a Rating of 4.9/5.

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!

User ID: 9***72 United States

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.

User ID: 4***24 United States

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.

User ID: 4***24 United States

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.

User ID: 4***24 United States

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.

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.

User ID: 8***69 United Arab Emirates

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

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

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

User ID: 3***56 Qatar

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

User ID: 7***33 United States

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.

User ID: 5***56 United States

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.

User ID: 5***56 United States

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

User ID: 9***77 Great Britain

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.

User ID: 2***37 Australia

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.

User ID: 2***37 Australia

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

User ID: 2***37 Australia

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.

User ID: 1***967 Netherlands

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

User ID: 7***82 Australia

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 .

User ID: 1***76 Sweden

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!

User ID: 4***46 Australia

## 5% Cashback

On APP - grab it while it lasts!