Search This Blog

Sunday, 15 September 2013

Binary to Octal Conversion in C++

Digital Conversion From Binary 2 Octal

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>

void main()
{
  int ar[10],n,j=0,sum=0,dec,i=0;
  cout<<"Enter the binary value:";
  cin>>n;
  while(n>0)
  {
    sum=sum+n%10*pow(2,j);
    j++;
    n=n/10;
  }
  dec=sum;
  while(dec>0)
  {
    ar[i]=dec%8;
    i++;
    dec=dec/8;
  }
 cout<<"octal value is:";
 for(int k=i-1;k>=0;k--)
 {
    cout<<ar[k];
 }
  getch();
}

Output

No comments:

Popular Posts