Decimal to octal number conversion program in C++
Decimal to octal number conversion program in C++.
Decimal to octal
Decimal Value =115;
Integer | Quotient | Reminder | Reminder in octal |
115/8 | 14 | 3 | 3 |
14/8 | 1 | 6 | 6 |
1/8 | 0 | 1 | 1 |
Octal value = 163
Flowchart of Decimal to octal program in C++
C++ Program to convert Decimal to octal number program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<iostream> using namespace std; int main() { int n,j,ocn=0 ,i=1 ; cout<<"Please Enter the decimal number : "; cin>>n; j=n; do { ocn=ocn+(j%8)*i; i=i
*10; j=j/8; } while(j!=0); cout<<"Decimal is "<<n<<" and its octal is : "<<ocn; } |
Output
Please Enter the decimal number: 9
Decimal is 9 and its octal is: 11
Excercise
Find the possible mistakes in the following Shamil’s Flow Table of the Program to convert the decimal number to its octal number.
if user input decimal as 115 the loop will execute as follows
Loop | What lines will execute? | Actual work to do |
1 time
j=115 |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, | ocn=ocn(j%8)*i
ocn=0 +(163%8) *1 ocn=0+(3) *1 ocn=3
i=i*10 i=1*10 i=10 |
2 time
j=14 |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
9, 10, 11, 12, 13, 14, 15, 16, 17 |
ocn=ocn(j%8) *i
ocn=3 +(8%14) *10 ocn=3+(6) *10 0cn=63
i=i*10 i=10*10 i=100 |
3 time
j=1 |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
9, 10, 11, 12, 13, 14, 15, 9, 10, 11, 12, 13, 14, 15, 16, 17 |
ocn=ocn(j%8) *I
ocn=63+(1%8) *100 ocn=63+(1) *10 ocn=163
i=i*10 i=100*10 i=1000
the octal of 115 is 163 |