Decimal to Binary and Binary to Decimal Program in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Conversion from decimal to binary and vice-versa is a very important problem in computer science. We know that computer only understands binary numbers that are 0 and 1. Every data are given as input to the computer converts into a binary number system. In the same way, conversion of Binary to Decimal, Decimal to Octal and Octal to DecimalOctal to Binary and Binary to Octal also can be done.

C Program to Convert Decimal to Binary

 #include<stdio.h>
 int main()
 {
     int dec, bin=0, rem, i=1;

     printf("Enter Decimal number: ");
     scanf("%d",&dec);

     while(dec!=0)
     {
         rem = dec % 2;
         bin += (rem*i); // bin = bin + rem*i
         dec /= 2; //dec = dec / 2
         i *= 10; //i=i*10
     }

     printf("Binary= %d\n",bin);

     return 0;
 }

Output:-

Enter Decimal Number: 12
Binary Value= 1100

Using the function

A function is a block of code that performs a specific task. For example, the main is a function and every program execution starts from the main function in C programming.

The C program is made of one or more functions. Every program must have at least one function with the name main. The execution of the program always starts from the main function and ends with the main function. The main function can call other functions to do some special task.

The function is a small program is used to do a particular task. In C a big program divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

 #include<stdio.h>
 int convertToBinary(int n);
 int main()
 {
     int num, binary;

     printf("Enter Decimal Number: ");
     scanf("%d",&num);

     binary = convertToBinary(num);

     printf("Binary Value= %d\n",binary);

     return 0;
 }

 int convertToBinary(int n)
 {
     int rem, bin=0, i=1;

     while(n!=0)
     {
         rem= n%2;
         bin += (rem*i);
         n /= 2; 
         i *= 10;
     }

     return bin;
 }

C program to convert binary to decimal

This time we need to calculate power. So, we will use pow() function defined under standard library function math.h

 #include<stdio.h>
 #include<math.h>
 int main()
 {
     int bin, dec=0, rem, i=0;

     printf("Enter Binary number: ");
     scanf("%d",&bin);

     while(bin!=0)
     {
         rem = bin % 10;
         dec += (rem * pow(2,i));
         bin /= 10;
         i++;
     }

     printf("Decimal = %d ",dec);

     return 0;
 }

Output:-

Enter binary number: 11101
Decimal value=29

If you are using Linux/Unix OS and getting an error then read it “undefined reference to sqrt (or other mathematical functions)” even include math.h header

Using Function

 #include<stdio.h>
 #include<math.h>
 int convert(int bin);
 int main()
 {
     int binary, decimal;

     printf("Enter binary number: ");
     scanf("%d",&binary);

     decimal = convert(binary);

     printf("Decimal value=%d",decimal);

     return 0;
 }

 int convert(int bin)
 {
     int rem, i=0, dec=0;

     while(bin!=0)
     {
         rem = bin%10;
         dec += (rem*pow(2,i));
         bin /= 10;
         i++;
     }

     return dec;
 }

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Similar C programming examples 

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Comment

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