Wednesday, 24 October 2012

Adding two octal numbers in C


#include<stdio.h>
#include<conio.h>
void main()
{

  int d1,d2,r,q,n1=0,n2=0,n=0,sum=0;
  int o[100],i=0;
  clrscr();
  printf("Enter two decimal number: ");
  scanf("%d%d",&d1,&d2);
  q = d1;
  while(q!=0)
  {
      o[++i]= q % 8;
      q = q / 8;
      n++;
  }

  for(i=n;i>=1;i--)
  {
n1=o[i]+n1*10;
  }

  q = d2;
  i=0;
  n=0;
  while(q!=0)
  {
      o[++i]= q % 8;
      q = q / 8;
      n++;
  }
  for(i=n;i>=1;i--)
  {
n2=o[i]+n2*10;
  }
  sum=n1+n2;
  printf("\n\n\tThe decimal value %d equivalent octal number is %d",d1,n1);
  printf("\n\tThe decimal value %d equivalent octal number is %d",d2,n2);
  printf("\n\n\tSum of two octal number (%d+%d) is %d",n1,n2,sum);
 // printf("\n\n\tSum of two octal number %o + %o = %d",d1,d2,sum);
  getch();
}

No comments:

Post a Comment