C++ Program to Convert Number Octal to Binary

Question : Write a C++ C++ Program to Convert Number Octal to Binary?

Explain: C++ Language Question about conversion Number Octal to Binary with input from user.

The octal number is the base-8 number system, and uses the digits 0 to 7.

Formula:
Table of Octal to Binary 
Oct=Binary code
0=000
1=001     
2=010       
3=011      
4=100   
5=101   
6=110     
7=111

Solution:

 /************************************************************ 
     C++ Program for Convert Number Octal to Binary
 *************************************************************
***************** By www.cpplanguage.com *********************/

 #include<iostream>
using namespace std;
int main()
{
    char octNumber[500];
    int c=0;
    cout<<"Enter you octal number: ";
// getting input octal number
    cin>>octNumber;
    cout<<octNumber<<" = "<<"binary ";
    for(c=0;c<octNumber[c];c++)
    {
// applying formula oct value to binary 
        switch(octNumber[c])
          {
             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;
// Default output if not  range 0-7 octal number
              default:  cout<<"This is not a octal no"<<octNumber[c];
         return 0;
        }  
    }
    cout<<endl;
}

Output:
C++ Language Program for Convert Number Octal to Binary


No comments:

Post a Comment