C program to convert decimal to binary
In this post, you will learn how to convert a decimal into an equivalent binary number using the C programming language. Here, we have mentioned three ways to convert a decimal to a binary number.
A decimal number is a base-10 number system. It ranges from 0 to 9, i.e., 0,1,2,3,4,5,6,7,8,9. Any combination of digits is a decimal number, such as 61, 22, 912, 0, 5 etc. The main advantages of the decimal number system are that it is easy-to-read, used by humans, and easy to manipulate.
A binary number consists of two numbers, 0s and 1s. It is expressed in the base-2 numeral system or binary numeral system. As the computer only understands binary language, that is 0 or 1, all inputs given to a computer are decoded by it into series of 0's or 1's to process it further. Any combination of 0 and 1 is a binary number, such as 1011, 100, 101011, 111011 etc.
Decimal to binary in C using for loop
The given program converts decimal to binary using a for loop. First, we divided the number by 2 using the modulus operator and stored the remainder in an array. Next, we divide the number by 2 through the division operator and repeat the same process until the number is greater than zero.
/* C Program to convert decimal to binary */
#include <stdio.h>
int main()
{
int a[10], num, i, j;
printf("\nPlease enter the number : ");
scanf("%d", &num);
for(i = 0; num > 0; i++)
{
a[i] = num % 2;
num = num / 2;
}
printf("\nBinary number of the given number : ");
for(j = i - 1; j >= 0; j--) {
printf(" %d ", a[j]);
}
printf("\n");
return 0;
}
Output 1 -
Please enter the number : 25
Binary number of the given number = 1 1 0 0 1
Output 2 -
Please enter the number : 52
Binary number of the given number : 1 1 0 1 0 0
Decimal to binary in C using while loop
In the given C program, we have used the while loop to convert decimal to binary.
#include <stdio.h>
int main()
{
int a[10], num, i = 1, j;
printf("\nPlease enter the number : ");
scanf("%d", &num);
while(num != 0)
{
a[i++] = num % 2;
num = num / 2;
}
printf("\nBinary number of the given number : ");
for(j = i - 1; j > 0; j--) {
printf(" %d ", a[j]);
}
return 0;
}
Output 1
Please enter the number : 35
Binary number of the given number : 1 0 0 0 1 1
Output 2
ease enter the number : 22
Binary number of the given number : 1 0 1 1 0
Decimal to binary in C using function and for loop
In this program, we have created a user-defined function Decimal_to_Binary() and used for loop and the Bitwise AND Operator to convert a decimal to a binary number. The programs takes decimal number (entered by user) as input and converts it into a binary number using the function Decimal_to_Binary().
/* C Program to convert decimal to binary */
#include <stdio.h>
void Decimal_to_Binary(int num)
{
int y;
for(int x = 31; y >= 0; x--)
{
y = num >> x;
if(y & 1)
printf("1");
else
printf("0");
}
}
int main()
{
int num;
printf("\n Please enter the number : ");
scanf("%d", &num);
Decimal_to_Binary(num);
return 0;
}
Output of the above code:
Please enter the number : 19
00000000000000000000000000010011
Decimal to binary in C using function and while loop
In the given C program, we have created a user-defined function decimal_to_binary() and used a while loop and Bitwise AND Operator to convert a decimal to a binary number. This program takes decimal number (entered by the user) as input and converts it into a binary number using the function decimal_to_binary().
#include <stdio.h>
#include <math.h>
long decimal_to_binary(int decimalnum)
{
long binarynum = 0;
int rem, temp = 1;
while (decimalnum!=0)
{
rem = decimalnum%2;
decimalnum = decimalnum / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
return binarynum;
}
int main()
{
int decimalnum;
printf("Enter a Decimal Number: ");
scanf("%d", &decimalnum);
printf("Equivalent Binary Number is: %ld", decimal_to_binary(decimalnum));
return 0;
}
Output of the above code:
Enter a Decimal Number: 67
Equivalent Binary Number is: 1000011
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative