This snippet has a will convert Decimal number to Octal number.
Covert Decimal to Octal
// coverts decimal to ocal
// vishesh
// 21/05/2005
#include <iostream>
char* DEC_OCT(int);
int main()
{
//
int num;
std::cout << "Enter a number: ";
std::cin >> num;
std::cout << DEC_OCT(num);
std::cin.get();
return 0;
}
char* DEC_OCT(int dec)
{
using namespace std;
char *rtn;
int prev, len, temp, *OCT;
prev = dec;
for(int i=0;prev!=0;i++)
{
prev = prev/8;
len=i;
}
OCT = new int[len+1];
prev = dec;
for(int i=0;prev!=0;i++)
{
OCT[i] = prev%8;
prev = prev/8;
len=i;
}
OCT[len+1] = prev%8;
len++;
for(int i=0;i<len/2;i++)
{
temp=OCT[i];
OCT[i]=OCT[len-1-i];
OCT[len-1-i]=temp;
}
rtn = new char[len+1];
for(int i=0;i<len;i++)
{
rtn[i]=OCT[i]+'0';
rtn[i+1]='\0';
}
return rtn;
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.