For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
Now Reading
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
The conversion of a decimal number (numbers between 0 – 9) into a binary number (numbers that only contain instances of 0 and 1) is possible using bitwise operators, stack and loops. Since binary values are entirely meant for the system’s comprehension, this conversion makes it simpler for humans to understand a decimal while converting it to a binary digit to enable system operation.
Before getting into the details of how to convert decimal to binary in C, let’s look at the overview of the number system.
The number system refers to a technique that represents the count or number of a given set of objects. For instance, how many fruits are in a basket? Essentially, it can be comprehended as a writing system for signifying numbers using symbols or digits. Various ways are available to denote a value.
Here are the 4 major types of number systems:
Number System | Base |
Binary System | 2 |
Octal System | 8 |
Decimal System | 10 |
Hexadecimal System | 16 |
A number system’s base indicates several unique digits it uses to showcase numbers. For example, base 8 indicates eight digits, i.e., 0, 1, 2, 3, 4, 5, 6 and 7. On the other hand, base 10 indicates 10 digits (0 to 10) to display numbers.
Binary number system refers to a numbering technique in computer architecture which only comprises two digits- 1 and 0. The system uses base 2 to denote numbers. So, each value in a binary number can be either 0 or 1. Examples of binary numbers are 100, 101, 111, etc.
On the other hand, the decimal system utilises base 10 to denote numbers. So, each value in a decimal number system can be one of these numbers: 0,1,2,3,4,5,6,7,8,9. Examples of decimal numbers include 45, 159, etc.
You need to use 0 and 1 to represent binary numbers. In order to represent decimal numbers, you need to use numbers from 0 to 9.
The following table represents decimal numbers and relevant binary number representation.
Decimal Numbers | Binary Numbers |
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
Let’s take the following example to understand how to convert a decimal number to binary.
We will take the number 7 and understand why it is represented as 0111 in binary. In its binary code, the least significant digit is 1, and its place value is 20, which is 1. Similarly, the place value designated to each digit is added to show that 0111 (with base 2) = 0 + 4 + 2 + 1 = 7 (with base 10).
The process to convert decimal to binary in C follows the below algorithm.
Step 1: Determine the remainder by dividing the input number by 2. Save the remainder within an array.
Step 2: Divide the remainder value by 2.
Step 3: Iterate these two steps till the value your receive, as a result, exceeds zero.
Step 4: Print the resulting array in a reverse format to obtain the binary form of the numeric value.
To convert decimal to binary in C using for loop, we begin by allowing the user to enter any decimal number. The decimal number is stored in the variable decinum (the input number). We then use a for loop to do the conversion. The loop calculates the remainder of the decinum divided by 2. This remainder is stored in the binarynum array, and we divide the decinum by 2 to obtain the subsequent bit. This process iterates until decinum equates to zero.
After the for loop, the program iterates over the binarynum in reverse sequence and prints each bit of the binary number. Lastly, the program returns 0 to demonstrate successful implementation.
Here’s an example C program to convert decimal to binary using for loop.
#include <stdio.h> |
Output:
Please enter a decimal number: 5 |
Note: We have declared the binarynum array with a size of 32 because we assumed it to have a 32-bit binary representation. You can change the size as per your needs.
The first step in this method is to prompt the user to enter any decimal number and save it in decinum variable.
The while loop continues until decinum exceeds 0. Within the loop, we determine the remainder of decinum divided by 2. This step represents the least significant bit of the corresponding binary number.
The program then stores the remainder within the binarynum array, and divides decinum by 2 to obtain the next bit. Subsequently, it increments the index i to get the next position within the array. The process of decimal to binary in C using array continues until decinum becomes zero.
After the while loop completes, a for loop starts to iterate over the binarynum array in reverse sequence. The program prints all bits of the binary number. Lastly, the program returns 0 to show successful implementation. Note that the program for this conversion is identical to a program to convert decimal to binary in java.
Here’s the example program for decimal to binary conversion using a while loop:
#include <stdio.h> |
Output:
Please enter a decimal number: 7 |
This method lets you define a Stack structure to denote the stack data structure. The corresponding stack holds an array of integers and an ‘up’ index to continuously track the current topmost element.
The program prompts the user to enter a decimal number and store it in decinum, while the initialize function initializes the stack.
The program then employs a while loop for binary-to-decimal conversion. The loop continually divides the number with 2 while pushing the remainder over the stack. The process continues until decinum becomes 0.
The program then uses another while loop to display the binary form. The pop function pops each bit from the stack and prints it. This loop continues until the stack goes empty ( the isStackEmpty function helps to check this).
Here’s a C program to convert binary to decimal using stack:
#include <stdio.h> |
Output:
Please enter a decimal number: 8 |
The process to convert decimal to binary in C without an array doesn’t consume any additional room to contain the number’s binary representation. The binary form of the value is stored as a decimal number. However, be cautious of the long long integer overflow to be able to continue the process by the range of long long int.
You can implement this method using a while loop that checks until the number exceeds 0.
This method checks each bit of the number to determine its binary form. The computer system stores any number as binary. So, analysing every bit helps you to determine the desired binary representation.
You can implement this method using a for loop that runs from the most significant to the least significant bit (i.e. left to right).
We’ve discussed various methods to convert decimal to binary in C. This conversion offers a rudimentary understanding of number representation. Moreover, it illustrates the basic concepts of binary arithmetic that streamline efficient analysis and manipulation of binary data.
Going through the tutorial is one of the most recommended approaches to initiate your C learning process. Along with this, you can pursue upGrad’s Master of Science in Computer Science offered by Liverpool John Moores University and accelerate your career in the tech industry. By pursuing this course, aspirants can learn in-demand skills and elevate their software development careers.
Enroll now to jumpstart your journey with upGrad!
1. Can you convert negative decimal numbers to binary using the decimal-to-binary conversion method?
Yes, you can use the mentioned approach to convert decimal numbers to binary. For that, you may have to modify the code to manipulate the sign bit and use suitable data types (for example, two's complement representation or signed integers).
2. Is there a standard library function in C to convert decimal to binary?
No standard library function in C exists to convert decimal numbers to binary. You can use library functions like itoa or sprintf for this conversion. Such functions let you form an integer in the form of a binary string. Note that these functions might not be supported in all C compilers or may demonstrate platform-specific behaviours.
3. How to convert a floating-point decimal number to binary?
The conversion of floating-point decimal numbers to binary involves isolating the fractional and integer parts of a number and then converting them separately. The corresponding fractional part conversion may undergo an iterative process. For this conversion, different techniques and algorithms are available, such as the IEEE 754 standard for signifying floating-point numbers.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.