# C Program to compute binary equivalent of a given decimal number

The binary equivalent of a decimal is obtained by calculating the remainder of that number when divided by 2, until that number is no more divisible by 2. This process is done with the help of for loop, and in each iteration of for loop we calculate the remainder and stores in an array which is binary equivalent of a given number by the end of for loop.

# Source Code :

#include<stdio.h> int main() { int a[20],i,m,n,r; printf("\n Enter the decimal Integer : "); scanf("%d",&n); //step 1 m=n; //step 2 for(i=0;n>0;i++) //step 3 { r=n%2; //step 3 (i) a[i]=r; //step 3(ii) n=n/2; //step 3(iii) } printf("\n Binary equivalent of %d is: \t",m); for(i--;i>=0;i--) //step 5 printf("%d",a[i]); return 0; }

# Sample Test cases for Binary Equivalent of a Decimal:

Example 1: Enter the decimal Integer : 10 Binary equivalent of 10 is : 1010 Example 2: Enter the decimal Integer : 80 Binary equivalent of 80 is : 1010000

# Explanation:

Step 1. Program reads a decimal integer from the user into the variable named ‘n’.

Step 2. The value of ‘n’ is stored into variable ‘m’ since subsequent steps invlove in the modification of value of ‘n’.

Step 3. This for loop calculates the binary equivalent of a decimal until n>0 ,

(i) The remainder is stored in the variable ‘r’. The ‘r’ will hold either 0 or 1.

(ii) The remainder is stored into array

(iii) The number is then divided by 2.

Step 4. By the end of for loop the array a[i] contains the binary equivalent of a number.

Step 5. finally array contents are displayed which is binary equivalent of a given decimal number.

# More Insights:

1. How to convert decimal number to binary.

More Content : Explore more