C++ program to Convert Decimal to Octal




Online computer science courses to jumpstart your future.

/**
 * CONVERT A DECIMAL TO OCTAL IN C++ codebind.com
 * 
 */
  
#include <iostream>
#include<cmath>
using namespace std;

void DecToOct(int decnum){

  int digits=1;
  int howbig=1;

  // check to see how many digits
  while(1){
      if((8*howbig)>decnum){
          break;
        }else{
          howbig*=8;
          digits++;
        }
    }

  // using binary to get octal
  for(int i=digits;i>0;i--)
    cout<<((decnum>>((i-1)*3))&7);
}

void DecToQuat(int decnum){
  int digits=1;
  int howbig=1;

  // check to see how many digits
  while(1){
      if((4*howbig)>decnum){
          break;
        }else{
          howbig*=4;
          digits++;
        }
    }

  // using binary to get quat
  for(int i=digits;i>0;i--)
    cout<<((decnum>>((i-1)*2))&3);
}



int main(){

  int decimalnum;
  cout<<"Enter the decimal to be converted:";
  cin>>decimalnum;
  DecToOct(decimalnum);
  cout<<endl;
  DecToQuat(decimalnum);

  return 0;
}

/*
OUTPUT
Enter the decimal to be converted:16
20
*/

Different ways of Putting the above question

  • What’s an easy way to convert a Decimal to Octal in C++?
  • C++ Decimal/Octal/Hex conversion program?
  • Convert Octal to Decimal in C++?
  • In c++: convert decimal to hexa-decimal and also octal …
  • Converting decimal to binary, octal and – C++
  • Decimal To Octal Conversion In C++
  • CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*