**Decimal to binary program in c **is conversion program in c.

In this tutorial we are going to see what is decimal number, What is binary number, how to convert decimal to binary, Decimal to binary program in c.

**What is decimal number**

A numbers we use in everyday life from 0 to 9 are the decimal numbers

**What is binary number**

A number expressed in base 2 represented in 0 and 1 are binary numbers.

**How to convert decimal to binary**

Decimal to binary conversion : we divide the decimal number by 2 continually which gives reminder either 0 or 1 or until final result is 0.

**Decimal to binary formula : **

Simply divide binary number by 2 and write down reminder o or 1 which is out binary number

**Decimal to binary example **

**Concept you need to know from c**

1. While loop in c

2. Assignment and arithmetic operator in c

**Decimal to binary algorithm**

Decimal to binary in c

1. Start

2. Declare variable

3. Divide decimal number by 2 continuously

4. If result is 0 or 1 then store result as binary number

5. final result is zero then stop

6. binary number

7. End

**Decimal to binary program in c**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> main() { int dec,temp,i,j=1,binary=0; printf("Enter decimal number to convert to binary number\n"); scanf("%d",&dec); temp=dec; while(temp!=0) { i=temp%2; binary=binary+(i*j); temp=temp/2; j=j*10; } printf("Binary number of %d is %d\n",dec,binary); } |

**Output :**

Decimal to binary conversion in c

**Explanation :-**

**1. Start with header files**

**2. Write a main function**

**3. Declare variables**

dec => to store decimal number

i, j, temp => temporary variable

binary => to store binary number

**4. Take decimal number input from user**

scanf(“%d”,&dec);

=> dec = 5

**5. Copy decimal number to temporary variable**

temp = dec

**6. Main logic begins to convert decimal to binary**

**To divide number till result is 0 we use while loop to write this condition**

while(temp!=0)

**While loop iteration 1 :**

5 != 0 this condition true while executed

i=temp%2 => i = 1

binary=binary+(i*j) => binary = 1

temp=temp/2 => temp = 2

j=j*10 => j = 10

**While loop iteration 2 :**

2 != 0 this condition true while executed

i=temp%2 => i = 0

binary=binary+(i*j) => binary = 1

temp=temp/2 => temp = 1

j=j*10 => j = 100

**While loop iteration 3 :**

1 != 0 this condition true while executed

i=temp%2 => i = 1

binary=binary+(i*j) => binary = 101

temp=temp/2 => temp = 0

j=j*10 => j = 1000

**While loop iteration 4 :**

0 != 0 this condition false and while stop executing

**7. Print binary number**

printf(“Binary number of %d is %d\n”,dec,binary);

=> Binary number of 5 is 101

**Recommended posts **

## Leave a Reply