Program C++ to convert octal number into binary
Write a program in C++ to convert an octal number into binary.
Flowchart of C++ Program to convert an octal number into binary
C++ Program Source Code to convert an octal number into binary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include<iostream> using namespace std; int main() { int num,p=1; int decmal=0,i=1,j,d; int binary=0; cout<<"Enter a number in octal (using digit 0 - 7) :"; cin>>num; j=num; do { d = j % 10; if(i==1) p=p*1; else p=p*8; decmal=decmal+(d*p); i++; j=j/10; } while(j>0);
i=1; j=decmal; do { binary=binary+(decmal % 2)*i; i=i*10; decmal=decmal/2; j=j/2; } while(j>0); cout<<"Octal number into Binary number "<<binary; } |
Output
Enter a number in octal (between 0 – 7) : 6
Octal number into Binary number 110
Excercise
Find the possible mistakes in the following Shamil’s Flow Table of the program in C++ to convert an octal number into binary.
loop
num=57 |
Loop Condition | if condition | what line will execute | Actual work to do |
True | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 18, 19, 20, 21 |
d=j%10
d=57%10, d=7 p=p*1, p=1 decmal=decmal+ (d*p), decmal=7 i++, j=57/10 j=5 |
||
J=5>0 | True | False | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
18, 19, 20, 21, 22, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21 |
d=5%10, d=5
p=1*8, p=8 decmal=7+(5*8), decmal=47 i++, j=5/10 j=0 |
j=0>0 | False | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 18, 19, 20, 21, 22, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22,23, 24,25,26,27,28,29,30,31 |
binary=binary+(decmal % 2)*i
binary=0+(47%2)*i, binary=1 i=i*10, i=10 decmal=decmal/2, decmal=23 j=47/2, j=23 |
|
j=23>0 | True | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22,23,
24,25,26,27, 28,29, 30, 31, 32, 25,26,27,28,29, 30,31 |
binary=1+(23%2)*10
binary=11, i=100 decmal=23/2 decmal=11 j=23/2, j=11
|
|
j=11>0 | True | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22,23,
24,25,26,27,28,29,30,31,32,25,26,27,28,29,30,31,32,25,26,27,28,29,30,31 |
binary=11+(11%2)*100
binary=111, i=1000 decmal=11/2 decmal=5, j=11/2 j=5
|
|
j=5>0 | True | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22,23,
24,25,26,27,28,29,30,31,32,25,26,27,28,29,30,31,32,25,26,27,28,29,30,31 |
binary=111+(5%2)*1000
binary=1111, i=10000 decmal=5/2 decmal=2 j=5/2,  j=2 |
|
j=2>0 | True | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22,23,
24, 25, 26, 27, 28, 29, 30, 31,32, 25, 26, 27, 28, 29, 30, 31, 32, 25, 26,27, 28, 29, 30, 31, 32, 25, 26, 27, 28, 29, 30, 31, |
binary=1111+(2%2)*
10000 binary=1111, i=100000 decmal=2/2 decmal=1 j=2/2, j=1 |
|
j=1>0 | True | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22,23,
24,25, 26, 27, 28, 29, 30, 31, 32, 25, 26, 27, 28, 29, 30,31,32, 25, 26, 27,28, 29, 30, 31, 32, 25,26, 27, 28, 29, 30, 31, 32, 25, 26, Â 27, 28, 29, 30, 31, |
binary=1111+(1%2)*
100000 binary=101111, i=1000000 decmal=1/2 decmal=0 j=1/2, j=0
|
|
j=0>0 | False | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22,23,
24,25,26,27,28, 29,30,31,32,25, 26,27,28,29,30,31, 32,25,26,27,28, 29,30,31,32,25, 26, 27, 28, 29, 30, 31, 32, 25, Â 26, Â 27, 28, 29, 30, 31, 32, 33, 34 |
Print octal to binary
57=101111 |