Program in C++ to convert a decimal number into binary without using an array.

Write a program in C++ to convert a decimal number into binary without using an array.

INTEGER PART QUOTIENT REMAINDER REMAINDER IN QUOTIENT
4/2 2 0 0
2/2 1 0 0
1/2 0 1 1

(4)10  = (100)2

Flowchart of decimal number into binary without using an array

Flowchart of decimal number into binary without using an array
Figure: Flowchart of decimal number into binary without using an array

 

C++ code of decimal number into binary using do-while loop

Output

enter decimal no: 7

the binary no is: 11

Excercise

Find the possible mistakes in the following Shamil’s Flow Table of the program of decimal number into binary using a do-while loop.

Loop

 

Condition What lines will execute Actual work      

 

1 time

J=4

4>0

 

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16

 

J=n,n=4,i=1,bin=0

Bin=0+(4%2)*1=0

i=1*10=10

n=4/2=2

2 time

J=4

J=j/2=4/2

J=2

2>0

 

True 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16

17, 10, 11, 12, 13, 14, 15, 16

 

Bin=0+(2%2)*10=0

i=10*10=100

n=2/2=1

 

3 time

J=2

J=2/2

J=1

1>0

 

True  

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16

17, 10, 11, 12, 13, 14, 15, 16

17,10,11,12,13,14,15,16

 

Bin=0+(1%2)*100=100

i=100*10=1000

n=1/2=0

 

4 time

J=1

J=1/2

J=0

0>0

 

False

 

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16

17, 10, 11, 12, 13, 14, 15, 16

17,18,

19

 

Print binary =100

C++ code of decimal number into binary using while loop

Decimal number into binary using for loop in C++

Write a program in C++ to convert a decimal number into binary number using for loop.

Output
Please Enter the number to convert: 8
Binary of the given number= 1000

Latest posts by Prof. Fazal Rehman Shamil (see all)

Buy advertisement space on T4Tutorials

For more details email [email protected]