"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Wednesday, April 7, 2010

Write a c++ function that converts a 2 digit octal number into binary number and print the binary equivalent

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

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.