etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

Binary to decimal C program

In this post, you will learn how to convert a binary number into an equivalent decimal using the C programming language. Here, we have mentioned three ways to convert a binary to a decimal number.

A binary number consists of two numbers 0s and 1s. It is expressed in the base-2 numeral system or binary numeral system. As the computer only understands binary language that is 0 or 1, all inputs given to a computer are decoded by it into series of 0's or 1's to process it further. Any combination of 0 and 1 is a binary number such as 1011, 100, 101011, 111011 etc.





A decimal number is a base-10 number system. It ranges from 0 to 9 i.e., 0,1,2,3,4,5,6,7,8,9. Any combination of digits is a decimal number such as 61, 22, 912, 0, 5 etc. The main advantages of the decimal number system are easy-to-read, used by humans and easy to manipulate.



C convert binary to decimal using while loop

In the given C program, we ask the user to enter a number in binary number system to convert that number into decimal number system using the while loop.

#include <stdio.h>  

void main()  
{  
    // declaration of variables  
    int num, binary_num, decimal_num = 0, base = 1, rem;  
    printf("Enter a binary number: ");  
    scanf(" %d", &binary_num); 
    
    // print the binary number 
    printf("The binary number: %d \t", binary_num); 
      
    // convert binary to decimal 
    while(binary_num > 0)  
    {  
        /* divide the binary number by 10 and store the remainder in rem variable. */
        rem = binary_num % 10;   
        decimal_num = decimal_num + rem * base;  
        // divide the number with quotient  
        binary_num = binary_num / 10;
        base = base * 2;  
    }  
  
    // print the decimal
    printf("\nThe decimal number: %d \t", decimal_num); 
}  
Output of the above code:
Enter a binary number: 1101
The binary number: 1101 	
The decimal number: 13 	
Enter a binary number: 1110
The binary number: 1110 	
The decimal number: 14 




C convert binary to decimal using for loop

In the given C program, we ask the user to enter a number in binary number system to convert that number into decimal number system using the for loop.

#include <stdio.h>  

void main()  
{  
    // declaration of variables  
    int i,num, binary_num, decimal_num = 0, rem;  
    printf("Enter a binary number: ");  
    scanf(" %d", &binary_num); 
    
    // print the binary number 
    printf("The binary number: %d \t", binary_num); 
      
    // convert binary to decimal 
    for (i = 0; binary_num != 0; ++i)  
    {  
        rem = binary_num % 10;  
        binary_num = binary_num / 10;  
        decimal_num = decimal_num + (rem) * ( pow (2, i));  
          
    }  
  
    // print the decimal
    printf("\nThe decimal number: %d \t", decimal_num); 
}  
Output of the above code:
Enter a binary number: 1111
The binary number: 1111 	
The decimal number: 15 	
Enter a binary number: 11011
The binary number: 11011 	
The decimal number: 27




C convert binary to decimal using function

In the given C program, we create a custom function binaryTodecimal() to convert the user entered binary to decimal number.

#include <stdio.h>  
 
int binaryTodecimal(int bin_num);  
int main()  
{  
    // declare the local variable  
    int binary_num, dec_num;  
    printf("Enter a binary number: ");  
    scanf("%d", &binary_num);  
      
    // call the binaryTodecimal() function    
    dec_num = binaryTodecimal (binary_num); 
    printf("The decimal number: %d", dec_num);  
}  
  
// user-defined function 
int binaryTodecimal( int bin_num)  
{  
    // declaration of variables  
    int dec_num = 0, temp = 0, rem;  
    
    // convert binary to decimal 
    while (bin_num != 0)  
    {  
        rem = bin_num % 10;  
        bin_num = bin_num / 10;  
        dec_num = dec_num + rem * pow( 2, temp);  
        temp++;  
    }  
    return dec_num;  
}  
Output of the above code:
Enter a binary number: 11011
The decimal number: 27
Enter a binary number: 101011
The decimal number: 43




Related Articles

Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative




Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP Create Word Document from HTML
-----------------
How to get data from XML file in PHP
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP code to send email using SMTP
-----------------
Characteristics of a Good Computer Program
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
How to add multiple custom markers on google map
-----------------
How to get current directory, filename and code line number in PHP
-----------------
Fibonacci Series Program in PHP
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
How to generate QR Code in PHP
-----------------
Simple pagination in PHP
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
PHP MYSQL Advanced Search Feature
-----------------
PHP Server Side Form Validation
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
jQuery File upload progress bar with file size validation
-----------------
Simple PHP File Cache
-----------------
Simple File Upload Script in PHP
-----------------
Php file based authentication
-----------------
To check whether a year is a leap year or not in php
-----------------
Calculate distance between two locations using PHP
-----------------
PHP User Authentication by IP Address
-----------------
PHP Secure User Registration with Login/logout
-----------------
Simple way to send SMTP mail using Node.js
-----------------
How to print specific part of a web page in javascript
-----------------
Simple Show Hide Menu Navigation
-----------------
Detect Mobile Devices in PHP
-----------------
Polling system using PHP, Ajax and MySql
-----------------
PHP Sending HTML form data to an Email
-----------------
Google Street View API Example
-----------------
Get Visitor\'s location and TimeZone
-----------------
SQL Injection Prevention Techniques
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
Convert MySQL to JSON using PHP
-----------------
Set and Get Cookies in PHP
-----------------
CSS Simple Menu Navigation Bar
-----------------
PHP Programming Error Types
-----------------
Date Timestamp Formats in PHP
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
How to add google map on your website and display address on click marker
-----------------
Write a python program to print all even numbers between 1 to 100
-----------------
How to display PDF file in web page from Database in PHP
-----------------
PHP Getting Document of Remote Address
-----------------
File Upload Validation in PHP
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial






Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2023. All Rights Reserved.