Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Convert Decimal to Binary in C
Next article icon

Convert Binary to Decimal in C

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to write a C program to convert the given binary number into an equivalent decimal number. Binary numbers are expressed in base 2 ( 0, 1 ) and decimal numbers are expressed in base 10 ( 0-9 ).

Algorithm to Convert Binary Numbers to Decimal

  • The idea is to extract the last digit of the binary number by performing the modulo operation ( % ) and store it in a variable last_digit and remove the last digit from the binary number by dividing by 10.
  • Update the decimal value by multiplying last_digit with the current base value and adding it to dec.
  • Update the base value by multiplying it by 2 to represent the next power of 2 for the next digit.
  • Repeat these steps until are digits of the binary number are processed.
  • Return the variable dec that stores the decimal value.

The below diagram explains how to convert ( 1010 ) to an equivalent decimal value:

binary to decimal in c

C Program to Convert Binary Number to Decimal

C
#include <stdio.h>  int binaryToDecimal(int n) {     int dec = 0;      // Initializing base value to 1, i.e 2^0     int base = 1;          // Extracting each digits of binary number     // and adding corresponding exponent of 2     while (n) {         int last_digit = n % 10;         n = n / 10;          // Multiplying the last digit with the base value         // and adding it to the decimal value         dec += last_digit * base;          // Updating the base value by multiplying it by 2         base = base * 2;     }      return dec; }  int main() {     int num = 10101001;     printf("%d", binaryToDecimal(num));      return 0; } 

Output
169 

Time complexity: O(d), where d is the number of digits in binary number.
Auxiliary Space: O(1)

In the above program, we represented a binary number as integer value with base 10 as binary numbers are not directly supported by C language. One more common representation of binary number is in the form of strings. If the binary number is in the form of string, then the above program can be modified as shown:

C
#include <stdio.h> #include <string.h>  int binaryToDecimal(const char* binary) {     int dec = 0;          // Get length of binary string     int length = strlen(binary);          // Initializing base value to 1, i.e 2^0     int base = 1;          // Process from right to left (least significant to     // most significant bit)     for (int i = length - 1; i >= 0; i--) {                  // If current bit is '1'         if (binary[i] == '1') {             dec += base;         }                  // Update base for next position (multiply by 2)         base = base * 2;     }          return dec; }  int main() {     const char* binary = "10101001";     printf("%d\n", binaryToDecimal(binary));          return 0; } 

Output
169 

Time complexity: O(d), where d is the number of digits in binary number.
Auxiliary Space: O(1)

Refer to the complete article Program for Binary To Decimal Conversion for more details!



Next Article
Convert Decimal to Binary in C
author
kartik
Improve
Article Tags :
  • C Language
  • C Programs
  • C Conversion Programs

Similar Reads

  • Convert Decimal to Binary in C
    In this article, we will learn to write a C program to convert a decimal number into a binary number. The decimal number system uses ten digits from 0 to 9 to represent numbers and the binary number system is a base-2 number system that uses only 0 and 1 to represent numbers. Algorithm to Convert De
    2 min read
  • Convert a Char Array to Double in C
    Converting a char array to a double is a common operation in C programming. It involves taking a string of characters that represent a numerical value and converting it to a double-precision floating-point value. This can be done by using various approaches listed below: Convert char array into doub
    4 min read
  • C Program to Convert Decimal to Octal
    Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent octal number. i.e convert the number with base value 10 to base value 8. The base value of a number system determines the number of digits used to represent a numeric value. For example
    3 min read
  • Convert String to int in C
    In C, we cannot directly perform numeric operations on a string representing a numeric value. We first need to convert the string to the integer type. In this article, we will discuss different ways to convert the numeric string to integer in C language. Example: Input: "1234"Output: 1234Explanation
    6 min read
  • Converting String to Long in C
    Here, we will see how to build a C Program For String to Long Conversion using strtol() function. Syntax: long int strtol(char *string, char **ptr, int base)The first argument is given as a stringThe second argument is a reference to an object of type char*The third argument denotes the base in whic
    4 min read
  • C Program For Octal to Decimal Conversion
    The number system is one of the ways to represent numbers. Every number system has its own base or radix. For example, Binary, Octal, Decimal, and Hexadecimal Number systems are some of the number systems and are also used in microprocessor programming. These numbers are easy to convert from one sys
    3 min read
  • C Program For Hexadecimal to Decimal Conversion
    Here we will build a C program for hexadecimal to decimal conversion using 5 different approaches i.e. Using format SpecifierUsing Switch caseUsing array Using while loopUsing for loop We will keep the same input in all the mentioned approaches and get an output accordingly. Input:  hexanumber = "2D
    4 min read
  • C Program For Decimal to Hexadecimal Conversion
    Here we will build a C Program For Decimal to Hexadecimal Conversion using 4 different approaches i.e. Using format specifierUsing modulus division operatorWithout using the modulus division operatorUsing Functions We will keep the same input in all the mentioned approaches and get an output accordi
    3 min read
  • How to Convert an Integer to a String in C?
    Write a C program to convert the given integer value to string. Examples Input: 1234Output: "1234"Explanation: The integer 1234 is converted to the string "1234". Input: -567Output: "-567"Explanation: The integer -567 is converted to the string "-567". Different Methods to Convert an Integer to a St
    4 min read
  • C Program For Double to String Conversion
    To convert double to string in C language, we will use the sprintf function as follows: Input: n = 456321.7651234 Output: string: 456321.7651234 Method: Using sprintf By specifying the precision in sprintf, we can convert double to string or character array with custom precision. We can use sprintf
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences