In this tutorial, we will learn to write a program in c to convert a decimal number into a binary number. Here we will use the division concept to convert it into a binary number.
Before moving towards the writing program let’s know about the binary numbers.
What is the Binary Number?
A binary number is a number that is expressed in the base-2 numeral system. This expression contains only numbers 0 and 1.
For eg:
If we want to write decimal in binary
1 can be written as 0 0 0 1 = 2^0
2 can be written as 0 0 1 0 = 2^1
3 can be written as 0 0 1 1 = 2^1 + 2^0
4 can be written as 0 1 0 0 = 2^2
How our binary number conversion program will behave?
- Binary number conversion program in C will take decimal as an input.
- We will divide the number by 2 to find the remainder and store that remainder.
- After storing reminder replace the original number with the new number which we got after divided by two.
- This process will continue till original number become 0.
- Here remaindar is binary number which we will get everytime.
C Program to convert decimal number into binary
#include <stdio.h>
int main()
{
int arr[10], num, i, j;
printf("Program to Convert Decimal Number into Binary\n");
printf("Please Give a Number to Convert in Binary: ");
scanf("%d", &num);
printf("Binary Number of %d is = ",num);
for(i = 0; num > 0; i++)
{
arr[i] = num % 2;
num = num / 2;
}
for(j = i - 1; j >= 0; j--) {
printf(" %d ", arr[j]);
}
printf("\n");
return 0;
}
Output
Program to Convert Decimal Number into Binary
Please Give a Number to Convert in Binary: 23
Binary Number of 23 is = 1 0 1 1 1
Also Prepare Below Important Question
- Python Program to add two number using Recursion
- Python Program to Find Highest Frequency Element in Array
- Python Program to Merge two Arrays
- Perform left rotation by two positions in Array Using Python
- Python Program to Delete Element at Given Index in Array
- Python Program to Delete element at End of Array
- Python Program to Copy one String to Another String
- Python Program to Remove Repeated Character from String
- Print highest frequency Character in String in Python
- Python Program to Convert lowercase vowel to uppercase
- Convert Celsius to Fahrenheit in Python
- Leap Year Program in Python
- Python Program to convert Decimal to Octal number
- Python Program to Convert Decimal Number into Binary
- Find all Pairs Whose Sum is Equal to Given number in Python
- Python Program to Replace First Vowel With ‘-‘ in String
- Python Program to find Prime factors of given integer
- Python Program to Print Prime Number in given range
- Java Program to Perform Left Rotation on Array Elements by Two
- Java Program to Perform Right Rotation on Array Elements by Two
Interview Questions Categories
C Programming Interview Preparation
Core Java Programming Interview Preparation
- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions