-
Notifications
You must be signed in to change notification settings - Fork 3
/
88thProgram_DecimalToOctal.cpp
85 lines (81 loc) · 1.91 KB
/
88thProgram_DecimalToOctal.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*********
* Decimal to Octal:
* Given a decimal number, convert it to Octal number.
* ************************************************************************/
#include<iostream>
using namespace std;
int main(){
int num ;
cout << "Enter the number : ";
cin >> num;
int temp = num;
int temp2 = num;
int c = 0;
int base =1;
int octal = 0;
int rem;
while(temp!=0){
temp = temp/8;
c = c+1;
}
for(int i=1; i<=c; i++){
rem = temp2%8;
octal = octal + rem*base;
temp2 = temp2/8;
base = base*10;
}
cout << "Octal Value of :" << num<< "is :" <<octal << "\n";
return 0;
}
/****************************************************************
* Working of the above snippet:
* ***************************************************************
* num = 8 , temp = temp2 = num =8;
* c = 0;
* base =1;
* octal = 0;
* while(temp!=0){
* temp = 8/2 = 4;
* c = 0+1 =1;
*
* Again,
* temp = 4/2 = 2;
* c = 1+1 =2;
* Again,
* temp = 2/2 = 1;
* c = 2+1 =3;
* Again,
* temp = 1/2 = 0;
* c = 3+1 =4;
*
* }
*
* for int i = 1 to 4 ;
* i = 1;
* rem = 8%2 = 0;
* octal = 0 + 0 x 1 =0;
* temp2 = 8/8 = 1;
* base = 1*10 = 10;
*
* i =2;
* rem = 1%2 = 1;
* octal = 0 + 1 x 10 =10;
* temp2 = 1/8 = 0;
* base = 10*10 = 100;
*
* i =3;
* rem = 0%2 = 0;
* octal = 10 + 0 x 100 =10;
* temp2 = 0/8 = 0;
* base = 10*10*10 = 1000;
*
* i =4;
* rem = 0%2 = 0;
* octal = 10 + 0 x 1000 =10;
* temp2 = 0/8 = 0;
* base = 10*10*10 = 1000;
*
* Hence octal = 10
* Note : Base of Octal is 8 and Decimal is 10.
*
* ****************************************************************/