# Converting decimal to octal

This is a simple program converting user input decimal numbers into octal ones.

#include <iostream>
using namespace std;
main()
{
int de,oc,y,i=1,octal;
float decimal,deci,x;
cout<<"Enter decimal no :: ";
cin>>decimal;
de=decimal;
deci=decimal-de;
cout<<"("<<decimal<<")10 = (";
while(de>0)
{
oc=de%8;
de=de/8;
octal=octal+(oc*i);
i=i*10;
}cout<<octal<<".";
while(deci>0)
{
x=deci*8;
y=x;
deci=x-y;
cout<<y;
}
cout<<")8";
}

• thanks a lot it is a correct program i needed this program its very helpful to me – Muhammad Usama Nov 4 '18 at 16:27

Just, i don't understand why?

What's wrong with (for integer):

#include <iostream>
int main() {
int input;
std::cout << "Enter decimal number: ";
std::cin >> std::dec >> input;
std::cout << '\n' << std::oct << input << '\n';
}


• Avoid using namespace std, it's a bad practice
• Just main() isn't a valid prototype for the entry point, at least use int main()
• Define variables in the closest scope possible.
• Try naming variable consistently
• de=de/8; can be simplified to de /= 8;
• floating point conversion is a bit more tricky than just doing what you do

Edit: After testing it (and correcting main() and) : Your code just doesn't work, even for an integer value

Enter decimal no :: (0)10 = (32767.)8
Enter decimal no :: (8)10 = (32776.)8
Enter decimal no :: (10)10 = (32779.)8


Test yourself

• i have checked it on dev c++ its working 100%. and i have checked your method that was great but can you tell me how it will work for fraction. – Mubashir Hussain Rao Oct 29 '18 at 14:16
• if you want to check my program just copy it and paste in dev c++ than tell me if there is any bug. – Mubashir Hussain Rao Oct 29 '18 at 14:23
• which compiler do you use? which version? which flag? Allow me to doubt a bit – Calak Oct 29 '18 at 14:35
• using namespace std; is only bad in header files, not regular code. In fact, using it is the whole point of having namespaces in the first place. Otherwise you could just rename everything in std namespace as std_something and remove namespaces from the language. – merlyn Oct 29 '18 at 14:56
• Do you know about namespaces? Theirs purposes? You Don't want to expose the whole std namespace to use it. And here, if he really want to use it, he just have to put using namespace std;inside the main, at the top. Here he dont have collision (He dodged std::oct and std::dec`), but check out this tread – Calak Oct 29 '18 at 15:18