• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About Us
    • About CSEStack:
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • Campus Ambassador
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • C/C++
    • Python
    • Java
    • HTML CSS
    • SQL
  • CSE Subject
    • Compiler Design
    • Computer Network
    • COA
    • Data Structure
    • DBMS
    • Operating System
    • Theory of Automata
    • Web Technology
  • Linux
  • Trend
    • Artificial Intelligence (AI)
    • Big Data
    • Cloud Computing
    • Machine Learning (ML)
  • GATE CSE 2020
    • Self Study Plan
    • Complete Syllabus
    • FREE Test Series
    • Topper Interview (AIR 15)
    • Recommended Books by Topper
  • Career
    • Placement Interview
    • Jobs
    • Aptitude
    • Quiz
  • Material
    • Recommended Books
    • Software Installation
  • Contribute to Us
    • Write for Us
    • Submit Source Code or Program
    • Share Interview Experience
  • Tools

How to convert Decimal to Binary in C Program? [Complete Code]

Aniruddha Chaudhari/20 Sep, 15/4851/0
C / C++Code

If you are searching for inbuild function to convert the given integer value into binary format, let me clear it is not there. In fact, there is no boolean data type in C. So we have to convert decimal to Binary in C manually.

In many of the programming cases, you need to perform bitwise operations on the Binary format of an integer value. For example, you want to count a number of bits in the binary representation of the given number.

So, in C programming language, many times, we need to convert the decimal number to binary number before performing some bit manipulation operations.

C Program to Convert Decimal to Binary:

Following is the code snippet that convert Decimal to Binary in C.


#include<stdio.h>
//Function takes input as integer 
//and return integer value in binary format
unsigned intToBin(unsigned k)
{
	if (k == 0) return 0;
	if (k == 1) return 1;
	return (k % 2) + 10 * intToInt(k / 2);
}

void main()
{
	int k = 34;
	char str[100] = {0};
	printf("%d =%u",k,intToBin(k));
}

In this above code, I am using recursion programming technique. You can also use a loop and break statement.

Output:

100010

In this code for 34, you are getting the output as 100010.

Note: Both the input and output of the variable of the function intToBin() are an integer data type. You can save the binary representation of the number either in an integer or a string data type variable.

This is one of the tricky questions asked in many job interview rounds. You can find out more such kind of questions in the list of complete coding interview questions.

There are so many methods you can use to convert decimal to an integer. If you have any doubt or if you can suggest any other efficient method of doing this conversion, discuss in the comment section below.

bit manipulationCodecpp

Related Posts

C / C++Code

Nested Switch Statements in C Programming with Real Life Example

C / C++Code

Split String in C programming using strtok_r() | Parsing CSV File

C / C++CodeJAVAPython

[Explained in Detail] if else nested Programming Example in C, Java and Python

Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack Portal.

Leave a Reply Cancel reply

Interview Experiences

AccentureAgriChain Akamai Amazon Amdocs American Express Attra Infotech BARC Barclays BlockGrain BYJUS Cisco Cognizant Coupon Dunia Credit Suisse DE Shaw Druva Experis IT Eze Software Factset Fiorano General Electric Incture Technologies Infosys Kasmo Cloud Microsoft MindTree Mu Sigma Numerify Opteamix Oracle Persistent Pole to Win Qualcomm Reliance Riverbed Syntel TCS Tech Mahindra Teradata Terralogic Virtusa Wipro

Interview Questions



You can share your interview experience.

Don’t Miss !

Latest Articles

Cisco Online Test Pattern and Interview Questions for Freshers


[7 Best Tips] How to Make Daily Study Timetable and Stick to It?


Persistent Written Test and Technical Coding Questions [Paper Pattern]


10 Top Website Design Tips for Small Business to Boost Up Your Sales


Main Difference Between remove del and pop in Python List


Tech Mahindra Placement (Online Test | Essay Writing | Interview Questions)


Importance of Career Exploration for High-School Students | Why?


Favorite Topic

AI algorithm array bigdata bit manipulation career Code Computer Network cpp data analytics database data scientist Data Structure db DBMS difference between Django education GATE GATE Topper Interview HTML ibm IBM ISDL interview IP address Java JavaScript Jobs Linked List linux linux cmd nit OOPs Concept os Programming Python python list Qualcomm SOAP sorting stack string vim webbrowser web development

Contribution to Community

  • Contribute to CSEstack Portal
  • Submit Your Source Code or Program
  • Share Your Interview Experience
  • CSEStack Leadership Board
  • Campus Ambassador Program by CSEstack

About CSEstack Portal

  • About CSEStack:
  • Contact Us
  • CSEStack Campus Ambassador
  • Recommended Books by Expert

© 2019 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About Us
    • About CSEStack:
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • Campus Ambassador
  • LeaderBoard