Tuesday 19 May 2015

c++ program to convert a decimal number into octal equivalent

#include<iostream>
#include<stdlib.h>
#include<math.h>

using namespace std;

class octal
{
double oct[10];
int count;
public:
        octal(int x)
        {
                double a[10];
                int i=0,rem;

                while(x!=0)
                {
                        rem=x%8;
                        x=x/8;
                        a[i++]=rem;
                }
                count=i;
                int n=count-1;

                for(i=0;i<=count;i++)
                {
                        oct[i]=a[n];
                        n--;
                }
        }

        double operator+(double k)
        {
                double sum=0;
                int j,i;
                j=count-1;
                for(i=0;i<count;i++)
                {
                        sum=sum+oct[j]*pow(8,i);
                        j--;
                }
                return (sum+k);
        }
        friend ostream & operator <<(ostream &,octal &);
};

ostream & operator <<(ostream &print,octal &o)
{
        for(int i=0;i<o.count;i++)
        print<<o.oct[i];
        return (print);
}

int main()
{
        int x;
        double k,y;
        cout<<"enter double:\t";
        cin>>x;
        octal h=octal(x);
        cout<<"octal equvalient:\t"<<h;
        cout<<"\nenter an double to add with an octal:\t";
        cin>>k;
        y=h+k;
        cout<<"sum of h+k\t"<<y<<"\n";
        return 1;
}

No comments:

Post a Comment