This program converts any octal number to its binary equivalent
#include <iostream.h>
#include <conio.h>
#include<process.h>
#include<string.h>
void main()
{
char bina[30] = " ", temp[4] = " ", binar[30] = " ";
int oct;
int r;
cout << "\n Enter an octal number :";
cin >> oct;
int o = oct; // o is the backup of octal number
while(oct)
{
r = oct % 10;
oct = oct / 10;
switch(r)
{
case 1:
strcpy(temp, "001");
break;
case 2:
strcpy(temp, "010");
break;
case 3:
strcpy(temp, "011");
break;
case 4:
strcpy(temp, "100");
break;
case 5:
strcpy(temp, "101");
break;
case 6:
strcpy(temp, "110");
break;
case 7:
strcpy(temp, "111");
break;
default :
cout << endl << o << " is not an octal number ";
getch();
exit(1);
}
strcpy( binar, bina);
strcpy( bina, temp);
strcat( bina, binar );
}
cout << "\n The binary equivalent of octal number " << o << " is " << bina ;
getch();
}
No comments:
Post a Comment