In this blog post, we learn how to write a C program to decimal to binary without using arithmetic operators?. We will write the C program to decimal to binary number without using arithmetic operators. Write a C program to input the decimal number and convert it to a binary number without using arithmetic operators. How to convert decimal to binary number in C programming without using arithmetic operators. Logic to convert decimal to binary number in C without using arithmetic operators.
Example,
Decimal Input: 5 Binary Output: 101 Decimal Input: 9 Binary Output: 1001
C program to decimal to binary number without using arithmetic operators:
The below program ask the user to enter the decimal number. After getting the value from the user it will convert the decimal number into a binary number.
#include <stdio.h>
#define CHAR_BITS 8 // size of character
#define INT_BITS (sizeof(int) * CHAR_BITS)
int main()
{
int num, index, i;
int bin[INT_BITS] = {0};
printf("Enter decimal number: ");
scanf("%d", &num);
//Array Index for binary number
index = (INT_BITS - 1);
while(index >= 0)
{
// to get the last binary digit of the number 'num'
// and accumulate it at the beginning of 'bin'
bin[index] = (num & 1);
//Decrement index
index--;
//Right Shift num by 1
num >>= 1;
}
//Display converted binary on the console screen
printf("Converted binary is: ");
for(i=0; i<INT_BITS; i++)
{
printf("%d", bin[i]);
}
return 0;
}
Output:
