-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoOctalAndBinary.cpp
More file actions
66 lines (53 loc) · 980 Bytes
/
toOctalAndBinary.cpp
File metadata and controls
66 lines (53 loc) · 980 Bytes
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
#include <bits/stdc++.h>
using namespace std;
// int DecimalToBinary(int n){
// int temp;
// int d=0;
// int p=0;
// int A[p];
// while(n>0){
// temp=n%2;
// p=p+1;
// A[p]=temp;
// n=n/2;
// }
// for(int i=p; p>0; p--){
// cout<<A[p]<<" ";
// }
// }
int decimalToBinary(int n){
int b=0;
int p=1;
int temp;
while(n>0){
temp=n%2;
b += (temp*p);
n /=2;
p*=10;
}
return b;
}
int decimalToOctal(int n){
int b=0;
int p=1;
int temp;
while(n>0){
temp=n%8;
b += (temp*p);
n /=8;
p*=10;
}
return b;
}
int main(){
int n;
cout<<"Decimal number: ";
cin>>n;
// int binary= DecimalToBinary(n);
// cout<<binary;
int num1 = decimalToOctal(n);
int num2 = decimalToBinary(n);
cout<<n<< " in octal is: "<<num1<<endl;
cout<<n<<" in binary is: "<<num2<<endl;
return 0;
}