Q:

C program to convert decimal to binary without using arithmetic operators

0

In this exercise, 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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

Enter decimal number: 12

Converted binary is: 00000000000000000000000000001100

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now