Decimal to Binary Conversion in C

Decimal to Binary Conversion in C. Decimal to Binary Conversion is a fundamental concept in computer science and programming. Decimal to binary conversion plays a very crucial role in various applications such as encoding, cryptography, and digital systems. In this article, we will write a C program that performs decimal to binary conversion.

Binary and Decimal Number System

Binary numbers represent values using powers of 2, where each digit is a power of 2 starting from the rightmost digit. while the decimal number system known as the base 10 system, is the standard system for integer and noninteger numbers. The decimal number system uses 10 digits- 0,1,2,3,4,5,6,7,8 and 9 to represent numerical values. In the decimal system, each digit position in a number is a power of 10.

Decimal to Binary Conversion Process

The Decimal to Binary conversion process involves dividing the decimal number by 2 and noting the remainder at each step. The remainders, read in reverse order form the binary equivalent of the decimal number.

C Program for Decimal to Binary Conversion


C Program for Decimal to Binary Conversion

Decimal to Binary Conversion Code Explanation

  • The code starts with including standard input output library stdio.h.
  • A function is declared with anme decimal to binary. This function takes an integer as an argument for decimal to binary conversion.
  • Inside function an arrray name binaryNumber is declared of size 32 to store binary digits.
  • Inside function counter i is used to keep track of index in binaryNumber array.
  • Inside the while loop, the function calculates the remainder when decimal number is divided by 2 and stores it in the binary number array.Then it updates the decimal number by dividing it by 2.
  • The while loop continues until deciaml number becomes 0.
  • After the while loop the function prints the binary equivalent by itterating through binaryNumber array in reverse order.
  • Inside main function program ask user to enter the number.
  • Main function calls deciamlToBinary function, passing the entered decimal number as an argument.

Happy Coding

See Also

1 thought on “Decimal to Binary Conversion in C”

Leave a Comment