-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOctalToBinary.cpp
More file actions
73 lines (57 loc) · 1.5 KB
/
OctalToBinary.cpp
File metadata and controls
73 lines (57 loc) · 1.5 KB
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
/*
Convert an Octal Number to a Binary Number
*/
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char octal[10];
int x,arr[10],len,i;
cout<<"Enter an Octal Number : ";
gets(octal); //receiving number in string format
for(i=0;octal[i]!='\0';i++)
if(octal[i]-'0'>=8) //to check if the entered number is in octal format
{
cout<<endl<<"Entered number is not in Octal Format!!!"<<endl;
goto lb;
}
else
arr[i]=octal[i]-'0'; //to convert char string into int array
len=strlen(octal);
cout<<endl<<"Binary Equivalent : ";
for(i=0;i<len;i++)
{
x=arr[i]; //handling one number at a time
switch(x) //Binary equivalent for each x
{
case 0:
cout<<"000 ";
break;
case 1:
cout<<"001 ";
break;
case 2:
cout<<"010 ";
break;
case 3:
cout<<"011 ";
break;
case 4:
cout<<"100 ";
break;
case 5:
cout<<"101 ";
break;
case 6:
cout<<"110 ";
break;
case 7:
cout<<"111 ";
break;
}
}
cout<<endl;
lb:
return 0;
}