sum of 2 numbers in 16 radix

sahasrara 0 Tallied Votes 138 Views Share

Dear programmer
This program get 2 (simply) numbers in 10 radix , then changes them to 16 radix and add together and finally shows result.

I wrote it by Borland 5.2 C++ .

Good Luck!

//sum of 2 numbers in 16 radix
//programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
//Date of send 2009/1/9

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

void main()
{
 int x,x1,y,y1,i;
 int d,n=1,m=1;
 int *r1,*r2,*sum;
 char h[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
 cout<<"Enter x=";   cin>>x;   x1=x;
 cout<<"Enter y=";   cin>>y;   y1=y;

  while(x >= 10)
 {
 	x/=10;
   ++n;
 }
 r1=new int[n];

  while(y >= 10)
 {
 	y/=10;
   ++m;
 }
 r2=new int[m];

 int max;
 if( n>= m)
  max=n;
 else
  max=m;

 sum=new int[max+1];

 for(i=0 ; x1 >= 16 ; ++i)
 {
 	r1[i] = x1 % 16;
   x1 /= 16;
 }
 r1[i++] = x1;

 for(i=0 ; y1 >= 16 ; ++i)
 {
 	r2[i]= y1 % 16;
   y1 /= 16;
 }
 r2[i++] = y1;

 cout<<"x in 16 radix = ";
 for(i=(n-1) ; i>=0 ; --i)
 	cout<<h[r1[i]];

 cout<<"\ny in 16 radix = ";
 for(i=(m-1) ; i>=0 ; --i)
 	cout<<h[r2[i]];

 int t=0;
 for(i=0 ; i<max ; ++i)
 {
   sum[i]=(r1[i] + r2[i] + t) % 16;
   t = (r1[i] + r2[i]) / 16;
 }

 cout<<"\nsum in y , x 16 radix = ";
 for(i=(max-1) ; i>=0 ; --i)
 	cout<<h[sum[i]];
 getch();
}
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.