C Program Converting Binary To Decimal Downloadbackstage




Decimal to binary in C to convert an integer from decimal number system (base-10) to binary number system (base-2). The size of an integer is assumed to be 32 bits. We use the bitwise operator 'AND' to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a for loop and bitwise AND the number obtained with 1(one) if the result is 1, then that bit is one otherwise zero (0).

  1. C++ Convert Decimal To Binary
  2. C Code Convert Binary To Decimal
  3. Vba Convert Binary To Decimal
  4. C++ Decimal To Binary Function

The following is a C program to convert a binary number to a decimal number. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24. C Program to Convert Binary Number to Decimal and vice-versa In this example, you will learn to convert binary numbers to decimal and vice-versa manually by creating a user-defined function. To understand this example, you should have the knowledge of the following C programming topics. Write a program that prompts the user to input a binary number and display its decimal equivalent. C Program to Convert Binary Number to Decimal Toggle navigation C Programming Notes.com.

C program to convert decimal to binary

#include <stdio.h>

int main()
{
int n, c, k;

printf('Enter an integer in decimal number systemn');
scanf('%d',&n);

printf('%d in binary number system is:n', n);

for(c =31; c >=0; c--)
{
k = n >> c;

if(k &1)
printf('1');
else
printf('0');
}

printf('n');

return0;
}

Output of the program:

Download Decimal binary program.

This code only prints binary of an integer. Still, we may wish to perform operations on binary, so in the program below, we store the binary in a string. We create a function that returns a pointer to it (the binary of the number passed).

Decimal to binary conversion in C

#include <stdio.h>
#include <stdlib.h>

char*decimal_to_binary(int);

int main()
{
int n;
char*p;

printf('Enter an integer in decimal number systemn');
scanf('%d',&n);

p = decimal_to_binary(n);
printf('Binary string of %d is: %sn', n, p);

free(p);

return0;
}

char*decimal_to_binary(int n)
{
int c, d, t;
char*p;

t =0;
p =(char*)malloc(32+1);

if(p NULL)
exit(EXIT_FAILURE);

C++ Convert Decimal To Binary

for(c =31; c >=0; c--)
{
d = n >> c;

if(d &1)
*(p+t)=1+'0';
else
*(p+t)=0+'0';

t++;
}
*(p+t)='0';

return p;
}

We allocate memory dynamically because we can't return a pointer to a local variable (character array in this case). If we return it to a local variable, then the program may crash, or we get an incorrect result.

In this article, you will learn and get code on binary to decimal conversion with and without using function in C++.

But before going through the program, if you are not aware about steps, formula, or logics used behind the conversion of binary to decimal, then you can refer to binary to decimal conversion. There you will get everything about it in very short time.

Binary to Decimal without Function

Convert decimal to binary algorithm

To convert any number entered by user (in binary number system) to its equivalent decimal value in C++ programming, you have to ask from user to enter the binary number first. And then apply the logic and convert it into its equivalent decimal value as shown in the program given below.

Let's have a look at the program first, will explain it later on.

This program was build and run under Code::Blocks IDE. Here is its sample run:

Now supply any input as binary number say 1111010 and press ENTER key to see its equivalent value in decimal as shown in the output given below:

C++ convert decimal to binary

Program Explained with Dry Run

The dry run of above program with user input 1111010 goes in a way that:

  1. Initially decnum=0, i=1 and binnum=1111010 (entered by user)
  2. The condition binnum!=0 (inside the while loop) or 1111010!=0 evaluates to be true
  3. Therefore program flow goes inside the loop
  4. And binnum%10 or 1111010%10 or 0 gets initialized to rem. Now rem=0
  5. Similarly, decnum+(rem*i) or 0+(0*1) or 0 gets initialied to decnum. Now decnum=0
  6. Then i*2 or 1*2 or 2 gets initialized to i. Now i=2
  7. And at last, binnum/10 or 1111010/10 or 111101 gets initialized to binnum. Now binnum=111101
  8. After executing all the four statements of while loop (for first time), we have decnum=0, i=2 and binnum=111101
  9. Program flow goes back to the condition of while loop
  10. Again the condition binnum!=0 or 111101!=0 evalutes to be true, therefore program flow again goes inside the loop
  11. And binnum%10 or 111101%10 or 1 gets initialized to rem. Now rem=1
  12. Similarly, decnum+(rem*i) or 0+(1*2) or 2 gets initialized to decnum. Now decnum=2
  13. And i*2 or 2*2 or 4 gets initialized to i. Now i=4
  14. And at last, binnum/10 or 111101/10 or 11110 gets initialized to binnum. Now binnum=11110
  15. Now process from step no.9 to 14 with new value of decnum, i and binnum
  16. Now we have decnum=2, i=8 and binnum=1111
  17. Go to step no.15
  18. Now we have decnum=10, i=16, and binnum=111
  19. Go to step no.15
  20. Now we have decnum=26, i=32, and binnum=11
  21. Go to step no.15
  22. Now we have decnum=58, i=64, and binnum=1
  23. Go to step no.15
  24. Now we have decnum=122, i=128, binnum=0

Dry Run in Tubular Form

While Loop Evaluationremdecnumibinnum
1st Evaluation002111101
2nd Evaluation12411110
3rd Evaluation0281111
4th Evaluation11016111
5th Evaluation1263211
6th Evaluation158641
7th Evaluation11221280

You can also print the value of all four variables say rem, decnum, i, and binnum during the execution of while loop by placing the following statement:

just before the while loop. And the following statement:

just after the fourth statement of while loop's body. Now with user input 1111010, output looks like:

Binary to Decimal using Function

This program converts binary to decimal using a user-defined function, BinToDec(). It takes binary number as its argument, and returns its equivalent decimal value.

C Code Convert Binary To Decimal

This program uses pow() function. It takes two argument, the first argument is known for base, whereas the second is known for exponent. Therefore, pow(2, 5) means 25, that equals, 32.

Vba Convert Binary To Decimal

This program produces the same output as of previous program. You can check with dry run on your own. To learn more about function in C++, you can refer its separate tutorial.

C++ Decimal To Binary Function

Same Program in Other Languages