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

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools

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

Aniruddha Chaudhari/14193/0
C / C++Code
YouTube CSEstack

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.

In C programming, to perform bit manipulation operations, many times, you need to convert the decimal number to binary number.

Here is a simple online tool to convert decimal to binary.

Now we want to write a C program for this conversion.

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 manipulationcpp
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a 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.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Function (Types/Call)
  7. C- strlen() vs sizeof()
  8. C- Nested Switch Statement
  9. C- Recursion
  10. C- Dynamic Programming
  11. C- Storage Classes
  12. C- Creating Header File
  13. C- Null Pointer
  14. C- Stack and Queue
  15. C- Implement Stack using Array
  16. C- Implement Linked List in C
  17. C- File Handling
  18. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2021 – CSEstack.org. All Rights Reserved.

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